linux/drivers/iio/light/adjd_s311.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * adjd_s311.c - Support for ADJD-S311-CR999 digital color sensor
   4 *
   5 * Copyright (C) 2012 Peter Meerwald <pmeerw@pmeerw.net>
   6 *
   7 * driver for ADJD-S311-CR999 digital color sensor (10-bit channels for
   8 * red, green, blue, clear); 7-bit I2C slave address 0x74
   9 *
  10 * limitations: no calibration, no offset mode, no sleep mode
  11 */
  12
  13#include <linux/module.h>
  14#include <linux/interrupt.h>
  15#include <linux/i2c.h>
  16#include <linux/slab.h>
  17#include <linux/delay.h>
  18#include <linux/bitmap.h>
  19#include <linux/err.h>
  20#include <linux/irq.h>
  21
  22#include <linux/iio/iio.h>
  23#include <linux/iio/sysfs.h>
  24#include <linux/iio/trigger_consumer.h>
  25#include <linux/iio/buffer.h>
  26#include <linux/iio/triggered_buffer.h>
  27
  28#define ADJD_S311_DRV_NAME "adjd_s311"
  29
  30#define ADJD_S311_CTRL          0x00
  31#define ADJD_S311_CONFIG        0x01
  32#define ADJD_S311_CAP_RED       0x06
  33#define ADJD_S311_CAP_GREEN     0x07
  34#define ADJD_S311_CAP_BLUE      0x08
  35#define ADJD_S311_CAP_CLEAR     0x09
  36#define ADJD_S311_INT_RED       0x0a
  37#define ADJD_S311_INT_GREEN     0x0c
  38#define ADJD_S311_INT_BLUE      0x0e
  39#define ADJD_S311_INT_CLEAR     0x10
  40#define ADJD_S311_DATA_RED      0x40
  41#define ADJD_S311_DATA_GREEN    0x42
  42#define ADJD_S311_DATA_BLUE     0x44
  43#define ADJD_S311_DATA_CLEAR    0x46
  44#define ADJD_S311_OFFSET_RED    0x48
  45#define ADJD_S311_OFFSET_GREEN  0x49
  46#define ADJD_S311_OFFSET_BLUE   0x4a
  47#define ADJD_S311_OFFSET_CLEAR  0x4b
  48
  49#define ADJD_S311_CTRL_GOFS     0x02
  50#define ADJD_S311_CTRL_GSSR     0x01
  51#define ADJD_S311_CAP_MASK      0x0f
  52#define ADJD_S311_INT_MASK      0x0fff
  53#define ADJD_S311_DATA_MASK     0x03ff
  54
  55struct adjd_s311_data {
  56        struct i2c_client *client;
  57        u16 *buffer;
  58};
  59
  60enum adjd_s311_channel_idx {
  61        IDX_RED, IDX_GREEN, IDX_BLUE, IDX_CLEAR
  62};
  63
  64#define ADJD_S311_DATA_REG(chan) (ADJD_S311_DATA_RED + (chan) * 2)
  65#define ADJD_S311_INT_REG(chan) (ADJD_S311_INT_RED + (chan) * 2)
  66#define ADJD_S311_CAP_REG(chan) (ADJD_S311_CAP_RED + (chan))
  67
  68static int adjd_s311_req_data(struct iio_dev *indio_dev)
  69{
  70        struct adjd_s311_data *data = iio_priv(indio_dev);
  71        int tries = 10;
  72
  73        int ret = i2c_smbus_write_byte_data(data->client, ADJD_S311_CTRL,
  74                ADJD_S311_CTRL_GSSR);
  75        if (ret < 0)
  76                return ret;
  77
  78        while (tries--) {
  79                ret = i2c_smbus_read_byte_data(data->client, ADJD_S311_CTRL);
  80                if (ret < 0)
  81                        return ret;
  82                if (!(ret & ADJD_S311_CTRL_GSSR))
  83                        break;
  84                msleep(20);
  85        }
  86
  87        if (tries < 0) {
  88                dev_err(&data->client->dev,
  89                        "adjd_s311_req_data() failed, data not ready\n");
  90                return -EIO;
  91        }
  92
  93        return 0;
  94}
  95
  96static int adjd_s311_read_data(struct iio_dev *indio_dev, u8 reg, int *val)
  97{
  98        struct adjd_s311_data *data = iio_priv(indio_dev);
  99
 100        int ret = adjd_s311_req_data(indio_dev);
 101        if (ret < 0)
 102                return ret;
 103
 104        ret = i2c_smbus_read_word_data(data->client, reg);
 105        if (ret < 0)
 106                return ret;
 107
 108        *val = ret & ADJD_S311_DATA_MASK;
 109
 110        return 0;
 111}
 112
 113static irqreturn_t adjd_s311_trigger_handler(int irq, void *p)
 114{
 115        struct iio_poll_func *pf = p;
 116        struct iio_dev *indio_dev = pf->indio_dev;
 117        struct adjd_s311_data *data = iio_priv(indio_dev);
 118        s64 time_ns = iio_get_time_ns(indio_dev);
 119        int i, j = 0;
 120
 121        int ret = adjd_s311_req_data(indio_dev);
 122        if (ret < 0)
 123                goto done;
 124
 125        for_each_set_bit(i, indio_dev->active_scan_mask,
 126                indio_dev->masklength) {
 127                ret = i2c_smbus_read_word_data(data->client,
 128                        ADJD_S311_DATA_REG(i));
 129                if (ret < 0)
 130                        goto done;
 131
 132                data->buffer[j++] = ret & ADJD_S311_DATA_MASK;
 133        }
 134
 135        iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, time_ns);
 136
 137done:
 138        iio_trigger_notify_done(indio_dev->trig);
 139
 140        return IRQ_HANDLED;
 141}
 142
 143#define ADJD_S311_CHANNEL(_color, _scan_idx) { \
 144        .type = IIO_INTENSITY, \
 145        .modified = 1, \
 146        .address = (IDX_##_color), \
 147        .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
 148                BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
 149                BIT(IIO_CHAN_INFO_INT_TIME), \
 150        .channel2 = (IIO_MOD_LIGHT_##_color), \
 151        .scan_index = (_scan_idx), \
 152        .scan_type = { \
 153                .sign = 'u', \
 154                .realbits = 10, \
 155                .storagebits = 16, \
 156                .endianness = IIO_CPU, \
 157        }, \
 158}
 159
 160static const struct iio_chan_spec adjd_s311_channels[] = {
 161        ADJD_S311_CHANNEL(RED, 0),
 162        ADJD_S311_CHANNEL(GREEN, 1),
 163        ADJD_S311_CHANNEL(BLUE, 2),
 164        ADJD_S311_CHANNEL(CLEAR, 3),
 165        IIO_CHAN_SOFT_TIMESTAMP(4),
 166};
 167
 168static int adjd_s311_read_raw(struct iio_dev *indio_dev,
 169                           struct iio_chan_spec const *chan,
 170                           int *val, int *val2, long mask)
 171{
 172        struct adjd_s311_data *data = iio_priv(indio_dev);
 173        int ret;
 174
 175        switch (mask) {
 176        case IIO_CHAN_INFO_RAW:
 177                ret = adjd_s311_read_data(indio_dev,
 178                        ADJD_S311_DATA_REG(chan->address), val);
 179                if (ret < 0)
 180                        return ret;
 181                return IIO_VAL_INT;
 182        case IIO_CHAN_INFO_HARDWAREGAIN:
 183                ret = i2c_smbus_read_byte_data(data->client,
 184                        ADJD_S311_CAP_REG(chan->address));
 185                if (ret < 0)
 186                        return ret;
 187                *val = ret & ADJD_S311_CAP_MASK;
 188                return IIO_VAL_INT;
 189        case IIO_CHAN_INFO_INT_TIME:
 190                ret = i2c_smbus_read_word_data(data->client,
 191                        ADJD_S311_INT_REG(chan->address));
 192                if (ret < 0)
 193                        return ret;
 194                *val = 0;
 195                /*
 196                 * not documented, based on measurement:
 197                 * 4095 LSBs correspond to roughly 4 ms
 198                 */
 199                *val2 = ret & ADJD_S311_INT_MASK;
 200                return IIO_VAL_INT_PLUS_MICRO;
 201        }
 202        return -EINVAL;
 203}
 204
 205static int adjd_s311_write_raw(struct iio_dev *indio_dev,
 206                               struct iio_chan_spec const *chan,
 207                               int val, int val2, long mask)
 208{
 209        struct adjd_s311_data *data = iio_priv(indio_dev);
 210
 211        switch (mask) {
 212        case IIO_CHAN_INFO_HARDWAREGAIN:
 213                if (val < 0 || val > ADJD_S311_CAP_MASK)
 214                        return -EINVAL;
 215
 216                return i2c_smbus_write_byte_data(data->client,
 217                        ADJD_S311_CAP_REG(chan->address), val);
 218        case IIO_CHAN_INFO_INT_TIME:
 219                if (val != 0 || val2 < 0 || val2 > ADJD_S311_INT_MASK)
 220                        return -EINVAL;
 221
 222                return i2c_smbus_write_word_data(data->client,
 223                        ADJD_S311_INT_REG(chan->address), val2);
 224        }
 225        return -EINVAL;
 226}
 227
 228static int adjd_s311_update_scan_mode(struct iio_dev *indio_dev,
 229        const unsigned long *scan_mask)
 230{
 231        struct adjd_s311_data *data = iio_priv(indio_dev);
 232
 233        kfree(data->buffer);
 234        data->buffer = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
 235        if (data->buffer == NULL)
 236                return -ENOMEM;
 237
 238        return 0;
 239}
 240
 241static const struct iio_info adjd_s311_info = {
 242        .read_raw = adjd_s311_read_raw,
 243        .write_raw = adjd_s311_write_raw,
 244        .update_scan_mode = adjd_s311_update_scan_mode,
 245};
 246
 247static int adjd_s311_probe(struct i2c_client *client,
 248                           const struct i2c_device_id *id)
 249{
 250        struct adjd_s311_data *data;
 251        struct iio_dev *indio_dev;
 252        int err;
 253
 254        indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
 255        if (indio_dev == NULL)
 256                return -ENOMEM;
 257
 258        data = iio_priv(indio_dev);
 259        i2c_set_clientdata(client, indio_dev);
 260        data->client = client;
 261
 262        indio_dev->info = &adjd_s311_info;
 263        indio_dev->name = ADJD_S311_DRV_NAME;
 264        indio_dev->channels = adjd_s311_channels;
 265        indio_dev->num_channels = ARRAY_SIZE(adjd_s311_channels);
 266        indio_dev->modes = INDIO_DIRECT_MODE;
 267
 268        err = iio_triggered_buffer_setup(indio_dev, NULL,
 269                adjd_s311_trigger_handler, NULL);
 270        if (err < 0)
 271                return err;
 272
 273        err = iio_device_register(indio_dev);
 274        if (err)
 275                goto exit_unreg_buffer;
 276
 277        dev_info(&client->dev, "ADJD-S311 color sensor registered\n");
 278
 279        return 0;
 280
 281exit_unreg_buffer:
 282        iio_triggered_buffer_cleanup(indio_dev);
 283        return err;
 284}
 285
 286static int adjd_s311_remove(struct i2c_client *client)
 287{
 288        struct iio_dev *indio_dev = i2c_get_clientdata(client);
 289        struct adjd_s311_data *data = iio_priv(indio_dev);
 290
 291        iio_device_unregister(indio_dev);
 292        iio_triggered_buffer_cleanup(indio_dev);
 293        kfree(data->buffer);
 294
 295        return 0;
 296}
 297
 298static const struct i2c_device_id adjd_s311_id[] = {
 299        { "adjd_s311", 0 },
 300        { }
 301};
 302MODULE_DEVICE_TABLE(i2c, adjd_s311_id);
 303
 304static struct i2c_driver adjd_s311_driver = {
 305        .driver = {
 306                .name   = ADJD_S311_DRV_NAME,
 307        },
 308        .probe          = adjd_s311_probe,
 309        .remove         = adjd_s311_remove,
 310        .id_table       = adjd_s311_id,
 311};
 312module_i2c_driver(adjd_s311_driver);
 313
 314MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
 315MODULE_DESCRIPTION("ADJD-S311 color sensor");
 316MODULE_LICENSE("GPL");
 317