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