tcs3472.c 9.2 KB

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