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