Peter Meerwald | eb869ad | 2013-09-14 20:38:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * tcs3472.c - Support for TAOS TCS3472 color light-to-digital converter |
| 3 | * |
| 4 | * Copyright (c) 2013 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 | * Color light sensor with 16-bit channels for red, green, blue, clear); |
| 11 | * 7-bit I2C slave address 0x39 (TCS34721, TCS34723) or 0x29 (TCS34725, |
| 12 | * TCS34727) |
| 13 | * |
| 14 | * TODO: interrupt support, thresholds, wait time |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/i2c.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/pm.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 TCS3472_DRV_NAME "tcs3472" |
| 29 | |
| 30 | #define TCS3472_COMMAND BIT(7) |
| 31 | #define TCS3472_AUTO_INCR BIT(5) |
| 32 | |
| 33 | #define TCS3472_ENABLE (TCS3472_COMMAND | 0x00) |
| 34 | #define TCS3472_ATIME (TCS3472_COMMAND | 0x01) |
| 35 | #define TCS3472_WTIME (TCS3472_COMMAND | 0x03) |
| 36 | #define TCS3472_AILT (TCS3472_COMMAND | 0x04) |
| 37 | #define TCS3472_AIHT (TCS3472_COMMAND | 0x06) |
| 38 | #define TCS3472_PERS (TCS3472_COMMAND | 0x0c) |
| 39 | #define TCS3472_CONFIG (TCS3472_COMMAND | 0x0d) |
| 40 | #define TCS3472_CONTROL (TCS3472_COMMAND | 0x0f) |
| 41 | #define TCS3472_ID (TCS3472_COMMAND | 0x12) |
| 42 | #define TCS3472_STATUS (TCS3472_COMMAND | 0x13) |
| 43 | #define TCS3472_CDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x14) |
| 44 | #define TCS3472_RDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x16) |
| 45 | #define TCS3472_GDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x18) |
| 46 | #define TCS3472_BDATA (TCS3472_COMMAND | TCS3472_AUTO_INCR | 0x1a) |
| 47 | |
| 48 | #define TCS3472_STATUS_AVALID BIT(0) |
| 49 | #define TCS3472_ENABLE_AEN BIT(1) |
| 50 | #define TCS3472_ENABLE_PON BIT(0) |
| 51 | #define TCS3472_CONTROL_AGAIN_MASK (BIT(0) | BIT(1)) |
| 52 | |
| 53 | struct tcs3472_data { |
| 54 | struct i2c_client *client; |
| 55 | u8 enable; |
| 56 | u8 control; |
| 57 | u8 atime; |
Peter Meerwald | 6a2f39b | 2013-09-15 16:52:00 +0100 | [diff] [blame] | 58 | u16 buffer[8]; /* 4 16-bit channels + 64-bit timestamp */ |
Peter Meerwald | eb869ad | 2013-09-14 20:38:00 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
Peter Meerwald | eb869ad | 2013-09-14 20:38:00 +0100 | [diff] [blame] | 61 | #define TCS3472_CHANNEL(_color, _si, _addr) { \ |
| 62 | .type = IIO_INTENSITY, \ |
| 63 | .modified = 1, \ |
| 64 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 65 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBSCALE) | \ |
| 66 | BIT(IIO_CHAN_INFO_INT_TIME), \ |
| 67 | .channel2 = IIO_MOD_LIGHT_##_color, \ |
| 68 | .address = _addr, \ |
| 69 | .scan_index = _si, \ |
Jonathan Cameron | 6b25f6e | 2013-12-11 18:45:00 +0000 | [diff] [blame] | 70 | .scan_type = { \ |
| 71 | .sign = 'u', \ |
| 72 | .realbits = 16, \ |
| 73 | .storagebits = 16, \ |
| 74 | .endianness = IIO_CPU, \ |
| 75 | }, \ |
Peter Meerwald | eb869ad | 2013-09-14 20:38:00 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static const int tcs3472_agains[] = { 1, 4, 16, 60 }; |
| 79 | |
| 80 | static const struct iio_chan_spec tcs3472_channels[] = { |
| 81 | TCS3472_CHANNEL(CLEAR, 0, TCS3472_CDATA), |
| 82 | TCS3472_CHANNEL(RED, 1, TCS3472_RDATA), |
| 83 | TCS3472_CHANNEL(GREEN, 2, TCS3472_GDATA), |
| 84 | TCS3472_CHANNEL(BLUE, 3, TCS3472_BDATA), |
| 85 | IIO_CHAN_SOFT_TIMESTAMP(4), |
| 86 | }; |
| 87 | |
| 88 | static int tcs3472_req_data(struct tcs3472_data *data) |
| 89 | { |
| 90 | int tries = 50; |
| 91 | int ret; |
| 92 | |
| 93 | while (tries--) { |
| 94 | ret = i2c_smbus_read_byte_data(data->client, TCS3472_STATUS); |
| 95 | if (ret < 0) |
| 96 | return ret; |
| 97 | if (ret & TCS3472_STATUS_AVALID) |
| 98 | break; |
| 99 | msleep(20); |
| 100 | } |
| 101 | |
| 102 | if (tries < 0) { |
| 103 | dev_err(&data->client->dev, "data not ready\n"); |
| 104 | return -EIO; |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int tcs3472_read_raw(struct iio_dev *indio_dev, |
| 111 | struct iio_chan_spec const *chan, |
| 112 | int *val, int *val2, long mask) |
| 113 | { |
| 114 | struct tcs3472_data *data = iio_priv(indio_dev); |
| 115 | int ret; |
| 116 | |
| 117 | switch (mask) { |
| 118 | case IIO_CHAN_INFO_RAW: |
| 119 | ret = tcs3472_req_data(data); |
| 120 | if (ret < 0) |
| 121 | return ret; |
| 122 | ret = i2c_smbus_read_word_data(data->client, chan->address); |
| 123 | if (ret < 0) |
| 124 | return ret; |
| 125 | *val = ret; |
| 126 | return IIO_VAL_INT; |
| 127 | case IIO_CHAN_INFO_CALIBSCALE: |
| 128 | *val = tcs3472_agains[data->control & |
| 129 | TCS3472_CONTROL_AGAIN_MASK]; |
| 130 | return IIO_VAL_INT; |
| 131 | case IIO_CHAN_INFO_INT_TIME: |
| 132 | *val = 0; |
| 133 | *val2 = (256 - data->atime) * 2400; |
| 134 | return IIO_VAL_INT_PLUS_MICRO; |
| 135 | } |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | |
| 139 | static int tcs3472_write_raw(struct iio_dev *indio_dev, |
| 140 | struct iio_chan_spec const *chan, |
| 141 | int val, int val2, long mask) |
| 142 | { |
| 143 | struct tcs3472_data *data = iio_priv(indio_dev); |
| 144 | int i; |
| 145 | |
| 146 | switch (mask) { |
| 147 | case IIO_CHAN_INFO_CALIBSCALE: |
| 148 | if (val2 != 0) |
| 149 | return -EINVAL; |
| 150 | for (i = 0; i < ARRAY_SIZE(tcs3472_agains); i++) { |
| 151 | if (val == tcs3472_agains[i]) { |
| 152 | data->control &= ~TCS3472_CONTROL_AGAIN_MASK; |
| 153 | data->control |= i; |
| 154 | return i2c_smbus_write_byte_data( |
| 155 | data->client, TCS3472_CONTROL, |
| 156 | data->control); |
| 157 | } |
| 158 | } |
| 159 | return -EINVAL; |
| 160 | case IIO_CHAN_INFO_INT_TIME: |
| 161 | if (val != 0) |
| 162 | return -EINVAL; |
| 163 | for (i = 0; i < 256; i++) { |
| 164 | if (val2 == (256 - i) * 2400) { |
| 165 | data->atime = i; |
| 166 | return i2c_smbus_write_word_data( |
| 167 | data->client, TCS3472_ATIME, |
| 168 | data->atime); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | |
| 177 | static irqreturn_t tcs3472_trigger_handler(int irq, void *p) |
| 178 | { |
| 179 | struct iio_poll_func *pf = p; |
| 180 | struct iio_dev *indio_dev = pf->indio_dev; |
| 181 | struct tcs3472_data *data = iio_priv(indio_dev); |
| 182 | int len = 0; |
| 183 | int i, j = 0; |
| 184 | |
| 185 | int ret = tcs3472_req_data(data); |
| 186 | if (ret < 0) |
| 187 | goto done; |
| 188 | |
| 189 | for_each_set_bit(i, indio_dev->active_scan_mask, |
| 190 | indio_dev->masklength) { |
| 191 | ret = i2c_smbus_read_word_data(data->client, |
| 192 | TCS3472_CDATA + 2*i); |
| 193 | if (ret < 0) |
| 194 | goto done; |
| 195 | |
| 196 | data->buffer[j++] = ret; |
| 197 | len += 2; |
| 198 | } |
| 199 | |
Lars-Peter Clausen | 0624bf8 | 2013-09-19 13:59:00 +0100 | [diff] [blame] | 200 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, |
| 201 | iio_get_time_ns()); |
Peter Meerwald | eb869ad | 2013-09-14 20:38:00 +0100 | [diff] [blame] | 202 | |
| 203 | done: |
| 204 | iio_trigger_notify_done(indio_dev->trig); |
| 205 | |
| 206 | return IRQ_HANDLED; |
| 207 | } |
| 208 | |
| 209 | static ssize_t tcs3472_show_int_time_available(struct device *dev, |
| 210 | struct device_attribute *attr, |
| 211 | char *buf) |
| 212 | { |
| 213 | size_t len = 0; |
| 214 | int i; |
| 215 | |
| 216 | for (i = 1; i <= 256; i++) |
| 217 | len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06d ", |
| 218 | 2400 * i); |
| 219 | |
| 220 | /* replace trailing space by newline */ |
| 221 | buf[len - 1] = '\n'; |
| 222 | |
| 223 | return len; |
| 224 | } |
| 225 | |
| 226 | static IIO_CONST_ATTR(calibscale_available, "1 4 16 60"); |
| 227 | static IIO_DEV_ATTR_INT_TIME_AVAIL(tcs3472_show_int_time_available); |
| 228 | |
| 229 | static struct attribute *tcs3472_attributes[] = { |
| 230 | &iio_const_attr_calibscale_available.dev_attr.attr, |
| 231 | &iio_dev_attr_integration_time_available.dev_attr.attr, |
| 232 | NULL |
| 233 | }; |
| 234 | |
| 235 | static const struct attribute_group tcs3472_attribute_group = { |
| 236 | .attrs = tcs3472_attributes, |
| 237 | }; |
| 238 | |
| 239 | static const struct iio_info tcs3472_info = { |
| 240 | .read_raw = tcs3472_read_raw, |
| 241 | .write_raw = tcs3472_write_raw, |
| 242 | .attrs = &tcs3472_attribute_group, |
| 243 | .driver_module = THIS_MODULE, |
| 244 | }; |
| 245 | |
| 246 | static int tcs3472_probe(struct i2c_client *client, |
| 247 | const struct i2c_device_id *id) |
| 248 | { |
| 249 | struct tcs3472_data *data; |
| 250 | struct iio_dev *indio_dev; |
| 251 | int ret; |
| 252 | |
| 253 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 254 | if (indio_dev == NULL) |
| 255 | return -ENOMEM; |
| 256 | |
| 257 | data = iio_priv(indio_dev); |
| 258 | i2c_set_clientdata(client, indio_dev); |
| 259 | data->client = client; |
| 260 | |
| 261 | indio_dev->dev.parent = &client->dev; |
| 262 | indio_dev->info = &tcs3472_info; |
| 263 | indio_dev->name = TCS3472_DRV_NAME; |
| 264 | indio_dev->channels = tcs3472_channels; |
| 265 | indio_dev->num_channels = ARRAY_SIZE(tcs3472_channels); |
| 266 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 267 | |
| 268 | ret = i2c_smbus_read_byte_data(data->client, TCS3472_ID); |
| 269 | if (ret < 0) |
| 270 | return ret; |
| 271 | |
| 272 | if (ret == 0x44) |
| 273 | dev_info(&client->dev, "TCS34721/34725 found\n"); |
| 274 | else if (ret == 0x4d) |
| 275 | dev_info(&client->dev, "TCS34723/34727 found\n"); |
| 276 | else |
| 277 | return -ENODEV; |
| 278 | |
| 279 | ret = i2c_smbus_read_byte_data(data->client, TCS3472_CONTROL); |
| 280 | if (ret < 0) |
| 281 | return ret; |
| 282 | data->control = ret; |
| 283 | |
| 284 | ret = i2c_smbus_read_byte_data(data->client, TCS3472_ATIME); |
| 285 | if (ret < 0) |
| 286 | return ret; |
| 287 | data->atime = ret; |
| 288 | |
| 289 | ret = i2c_smbus_read_byte_data(data->client, TCS3472_ENABLE); |
| 290 | if (ret < 0) |
| 291 | return ret; |
| 292 | |
| 293 | /* enable device */ |
| 294 | data->enable = ret | TCS3472_ENABLE_PON | TCS3472_ENABLE_AEN; |
| 295 | ret = i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE, |
| 296 | data->enable); |
| 297 | if (ret < 0) |
| 298 | return ret; |
| 299 | |
| 300 | ret = iio_triggered_buffer_setup(indio_dev, NULL, |
| 301 | tcs3472_trigger_handler, NULL); |
| 302 | if (ret < 0) |
| 303 | return ret; |
| 304 | |
| 305 | ret = iio_device_register(indio_dev); |
| 306 | if (ret < 0) |
| 307 | goto buffer_cleanup; |
| 308 | |
| 309 | return 0; |
| 310 | |
| 311 | buffer_cleanup: |
| 312 | iio_triggered_buffer_cleanup(indio_dev); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static int tcs3472_powerdown(struct tcs3472_data *data) |
| 317 | { |
| 318 | return i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE, |
| 319 | data->enable & ~(TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON)); |
| 320 | } |
| 321 | |
| 322 | static int tcs3472_remove(struct i2c_client *client) |
| 323 | { |
| 324 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 325 | |
| 326 | iio_device_unregister(indio_dev); |
| 327 | iio_triggered_buffer_cleanup(indio_dev); |
| 328 | tcs3472_powerdown(iio_priv(indio_dev)); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | #ifdef CONFIG_PM_SLEEP |
| 334 | static int tcs3472_suspend(struct device *dev) |
| 335 | { |
Peter Meerwald | 4caf279 | 2013-09-18 22:47:00 +0100 | [diff] [blame] | 336 | struct tcs3472_data *data = iio_priv(i2c_get_clientdata( |
| 337 | to_i2c_client(dev))); |
| 338 | return tcs3472_powerdown(data); |
Peter Meerwald | eb869ad | 2013-09-14 20:38:00 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static int tcs3472_resume(struct device *dev) |
| 342 | { |
Peter Meerwald | 4caf279 | 2013-09-18 22:47:00 +0100 | [diff] [blame] | 343 | struct tcs3472_data *data = iio_priv(i2c_get_clientdata( |
| 344 | to_i2c_client(dev))); |
Peter Meerwald | eb869ad | 2013-09-14 20:38:00 +0100 | [diff] [blame] | 345 | return i2c_smbus_write_byte_data(data->client, TCS3472_ENABLE, |
| 346 | data->enable | (TCS3472_ENABLE_AEN | TCS3472_ENABLE_PON)); |
| 347 | } |
| 348 | #endif |
| 349 | |
| 350 | static SIMPLE_DEV_PM_OPS(tcs3472_pm_ops, tcs3472_suspend, tcs3472_resume); |
| 351 | |
| 352 | static const struct i2c_device_id tcs3472_id[] = { |
| 353 | { "tcs3472", 0 }, |
| 354 | { } |
| 355 | }; |
| 356 | MODULE_DEVICE_TABLE(i2c, tcs3472_id); |
| 357 | |
| 358 | static struct i2c_driver tcs3472_driver = { |
| 359 | .driver = { |
| 360 | .name = TCS3472_DRV_NAME, |
| 361 | .pm = &tcs3472_pm_ops, |
| 362 | .owner = THIS_MODULE, |
| 363 | }, |
| 364 | .probe = tcs3472_probe, |
| 365 | .remove = tcs3472_remove, |
| 366 | .id_table = tcs3472_id, |
| 367 | }; |
| 368 | module_i2c_driver(tcs3472_driver); |
| 369 | |
| 370 | MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); |
| 371 | MODULE_DESCRIPTION("TCS3472 color light sensors driver"); |
| 372 | MODULE_LICENSE("GPL"); |