tcs3414.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * tcs3414.c - Support for TAOS TCS3414 digital color sensor
  3. *
  4. * Copyright (c) 2014 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. * Digital color sensor with 16-bit channels for red, green, blue, clear);
  11. * 7-bit I2C slave address 0x39 (TCS3414) or 0x29, 0x49, 0x59 (TCS3413,
  12. * TCS3415, TCS3416, resp.)
  13. *
  14. * TODO: sync, interrupt support, thresholds, prescaler
  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 TCS3414_DRV_NAME "tcs3414"
  26. #define TCS3414_COMMAND BIT(7)
  27. #define TCS3414_COMMAND_WORD (TCS3414_COMMAND | BIT(5))
  28. #define TCS3414_CONTROL (TCS3414_COMMAND | 0x00)
  29. #define TCS3414_TIMING (TCS3414_COMMAND | 0x01)
  30. #define TCS3414_ID (TCS3414_COMMAND | 0x04)
  31. #define TCS3414_GAIN (TCS3414_COMMAND | 0x07)
  32. #define TCS3414_DATA_GREEN (TCS3414_COMMAND_WORD | 0x10)
  33. #define TCS3414_DATA_RED (TCS3414_COMMAND_WORD | 0x12)
  34. #define TCS3414_DATA_BLUE (TCS3414_COMMAND_WORD | 0x14)
  35. #define TCS3414_DATA_CLEAR (TCS3414_COMMAND_WORD | 0x16)
  36. #define TCS3414_CONTROL_ADC_VALID BIT(4)
  37. #define TCS3414_CONTROL_ADC_EN BIT(1)
  38. #define TCS3414_CONTROL_POWER BIT(0)
  39. #define TCS3414_INTEG_MASK GENMASK(1, 0)
  40. #define TCS3414_INTEG_12MS 0x0
  41. #define TCS3414_INTEG_100MS 0x1
  42. #define TCS3414_INTEG_400MS 0x2
  43. #define TCS3414_GAIN_MASK GENMASK(5, 4)
  44. #define TCS3414_GAIN_SHIFT 4
  45. struct tcs3414_data {
  46. struct i2c_client *client;
  47. u8 control;
  48. u8 gain;
  49. u8 timing;
  50. u16 buffer[8]; /* 4x 16-bit + 8 bytes timestamp */
  51. };
  52. #define TCS3414_CHANNEL(_color, _si, _addr) { \
  53. .type = IIO_INTENSITY, \
  54. .modified = 1, \
  55. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  56. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  57. BIT(IIO_CHAN_INFO_INT_TIME), \
  58. .channel2 = IIO_MOD_LIGHT_##_color, \
  59. .address = _addr, \
  60. .scan_index = _si, \
  61. .scan_type = { \
  62. .sign = 'u', \
  63. .realbits = 16, \
  64. .storagebits = 16, \
  65. .endianness = IIO_CPU, \
  66. }, \
  67. }
  68. /* scale factors: 1/gain */
  69. static const int tcs3414_scales[][2] = {
  70. {1, 0}, {0, 250000}, {0, 62500}, {0, 15625}
  71. };
  72. /* integration time in ms */
  73. static const int tcs3414_times[] = { 12, 100, 400 };
  74. static const struct iio_chan_spec tcs3414_channels[] = {
  75. TCS3414_CHANNEL(GREEN, 0, TCS3414_DATA_GREEN),
  76. TCS3414_CHANNEL(RED, 1, TCS3414_DATA_RED),
  77. TCS3414_CHANNEL(BLUE, 2, TCS3414_DATA_BLUE),
  78. TCS3414_CHANNEL(CLEAR, 3, TCS3414_DATA_CLEAR),
  79. IIO_CHAN_SOFT_TIMESTAMP(4),
  80. };
  81. static int tcs3414_req_data(struct tcs3414_data *data)
  82. {
  83. int tries = 25;
  84. int ret;
  85. ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
  86. data->control | TCS3414_CONTROL_ADC_EN);
  87. if (ret < 0)
  88. return ret;
  89. while (tries--) {
  90. ret = i2c_smbus_read_byte_data(data->client, TCS3414_CONTROL);
  91. if (ret < 0)
  92. return ret;
  93. if (ret & TCS3414_CONTROL_ADC_VALID)
  94. break;
  95. msleep(20);
  96. }
  97. ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
  98. data->control);
  99. if (ret < 0)
  100. return ret;
  101. if (tries < 0) {
  102. dev_err(&data->client->dev, "data not ready\n");
  103. return -EIO;
  104. }
  105. return 0;
  106. }
  107. static int tcs3414_read_raw(struct iio_dev *indio_dev,
  108. struct iio_chan_spec const *chan,
  109. int *val, int *val2, long mask)
  110. {
  111. struct tcs3414_data *data = iio_priv(indio_dev);
  112. int i, ret;
  113. switch (mask) {
  114. case IIO_CHAN_INFO_RAW:
  115. ret = iio_device_claim_direct_mode(indio_dev);
  116. if (ret)
  117. return ret;
  118. ret = tcs3414_req_data(data);
  119. if (ret < 0) {
  120. iio_device_release_direct_mode(indio_dev);
  121. return ret;
  122. }
  123. ret = i2c_smbus_read_word_data(data->client, chan->address);
  124. iio_device_release_direct_mode(indio_dev);
  125. if (ret < 0)
  126. return ret;
  127. *val = ret;
  128. return IIO_VAL_INT;
  129. case IIO_CHAN_INFO_SCALE:
  130. i = (data->gain & TCS3414_GAIN_MASK) >> TCS3414_GAIN_SHIFT;
  131. *val = tcs3414_scales[i][0];
  132. *val2 = tcs3414_scales[i][1];
  133. return IIO_VAL_INT_PLUS_MICRO;
  134. case IIO_CHAN_INFO_INT_TIME:
  135. *val = 0;
  136. *val2 = tcs3414_times[data->timing & TCS3414_INTEG_MASK] * 1000;
  137. return IIO_VAL_INT_PLUS_MICRO;
  138. }
  139. return -EINVAL;
  140. }
  141. static int tcs3414_write_raw(struct iio_dev *indio_dev,
  142. struct iio_chan_spec const *chan,
  143. int val, int val2, long mask)
  144. {
  145. struct tcs3414_data *data = iio_priv(indio_dev);
  146. int i;
  147. switch (mask) {
  148. case IIO_CHAN_INFO_SCALE:
  149. for (i = 0; i < ARRAY_SIZE(tcs3414_scales); i++) {
  150. if (val == tcs3414_scales[i][0] &&
  151. val2 == tcs3414_scales[i][1]) {
  152. data->gain &= ~TCS3414_GAIN_MASK;
  153. data->gain |= i << TCS3414_GAIN_SHIFT;
  154. return i2c_smbus_write_byte_data(
  155. data->client, TCS3414_GAIN,
  156. data->gain);
  157. }
  158. }
  159. return -EINVAL;
  160. case IIO_CHAN_INFO_INT_TIME:
  161. if (val != 0)
  162. return -EINVAL;
  163. for (i = 0; i < ARRAY_SIZE(tcs3414_times); i++) {
  164. if (val2 == tcs3414_times[i] * 1000) {
  165. data->timing &= ~TCS3414_INTEG_MASK;
  166. data->timing |= i;
  167. return i2c_smbus_write_byte_data(
  168. data->client, TCS3414_TIMING,
  169. data->timing);
  170. }
  171. }
  172. return -EINVAL;
  173. default:
  174. return -EINVAL;
  175. }
  176. }
  177. static irqreturn_t tcs3414_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 tcs3414_data *data = iio_priv(indio_dev);
  182. int i, j = 0;
  183. for_each_set_bit(i, indio_dev->active_scan_mask,
  184. indio_dev->masklength) {
  185. int ret = i2c_smbus_read_word_data(data->client,
  186. TCS3414_DATA_GREEN + 2*i);
  187. if (ret < 0)
  188. goto done;
  189. data->buffer[j++] = ret;
  190. }
  191. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  192. iio_get_time_ns(indio_dev));
  193. done:
  194. iio_trigger_notify_done(indio_dev->trig);
  195. return IRQ_HANDLED;
  196. }
  197. static IIO_CONST_ATTR(scale_available, "1 0.25 0.0625 0.015625");
  198. static IIO_CONST_ATTR_INT_TIME_AVAIL("0.012 0.1 0.4");
  199. static struct attribute *tcs3414_attributes[] = {
  200. &iio_const_attr_scale_available.dev_attr.attr,
  201. &iio_const_attr_integration_time_available.dev_attr.attr,
  202. NULL
  203. };
  204. static const struct attribute_group tcs3414_attribute_group = {
  205. .attrs = tcs3414_attributes,
  206. };
  207. static const struct iio_info tcs3414_info = {
  208. .read_raw = tcs3414_read_raw,
  209. .write_raw = tcs3414_write_raw,
  210. .attrs = &tcs3414_attribute_group,
  211. .driver_module = THIS_MODULE,
  212. };
  213. static int tcs3414_buffer_preenable(struct iio_dev *indio_dev)
  214. {
  215. struct tcs3414_data *data = iio_priv(indio_dev);
  216. data->control |= TCS3414_CONTROL_ADC_EN;
  217. return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
  218. data->control);
  219. }
  220. static int tcs3414_buffer_predisable(struct iio_dev *indio_dev)
  221. {
  222. struct tcs3414_data *data = iio_priv(indio_dev);
  223. int ret;
  224. ret = iio_triggered_buffer_predisable(indio_dev);
  225. if (ret < 0)
  226. return ret;
  227. data->control &= ~TCS3414_CONTROL_ADC_EN;
  228. return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
  229. data->control);
  230. }
  231. static const struct iio_buffer_setup_ops tcs3414_buffer_setup_ops = {
  232. .preenable = tcs3414_buffer_preenable,
  233. .postenable = &iio_triggered_buffer_postenable,
  234. .predisable = tcs3414_buffer_predisable,
  235. };
  236. static int tcs3414_probe(struct i2c_client *client,
  237. const struct i2c_device_id *id)
  238. {
  239. struct tcs3414_data *data;
  240. struct iio_dev *indio_dev;
  241. int ret;
  242. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  243. if (indio_dev == NULL)
  244. return -ENOMEM;
  245. data = iio_priv(indio_dev);
  246. i2c_set_clientdata(client, indio_dev);
  247. data->client = client;
  248. indio_dev->dev.parent = &client->dev;
  249. indio_dev->info = &tcs3414_info;
  250. indio_dev->name = TCS3414_DRV_NAME;
  251. indio_dev->channels = tcs3414_channels;
  252. indio_dev->num_channels = ARRAY_SIZE(tcs3414_channels);
  253. indio_dev->modes = INDIO_DIRECT_MODE;
  254. ret = i2c_smbus_read_byte_data(data->client, TCS3414_ID);
  255. if (ret < 0)
  256. return ret;
  257. switch (ret & 0xf0) {
  258. case 0x00:
  259. dev_info(&client->dev, "TCS3404 found\n");
  260. break;
  261. case 0x10:
  262. dev_info(&client->dev, "TCS3413/14/15/16 found\n");
  263. break;
  264. default:
  265. return -ENODEV;
  266. }
  267. data->control = TCS3414_CONTROL_POWER;
  268. ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
  269. data->control);
  270. if (ret < 0)
  271. return ret;
  272. data->timing = TCS3414_INTEG_12MS; /* free running */
  273. ret = i2c_smbus_write_byte_data(data->client, TCS3414_TIMING,
  274. data->timing);
  275. if (ret < 0)
  276. return ret;
  277. ret = i2c_smbus_read_byte_data(data->client, TCS3414_GAIN);
  278. if (ret < 0)
  279. return ret;
  280. data->gain = ret;
  281. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  282. tcs3414_trigger_handler, &tcs3414_buffer_setup_ops);
  283. if (ret < 0)
  284. return ret;
  285. ret = iio_device_register(indio_dev);
  286. if (ret < 0)
  287. goto buffer_cleanup;
  288. return 0;
  289. buffer_cleanup:
  290. iio_triggered_buffer_cleanup(indio_dev);
  291. return ret;
  292. }
  293. static int tcs3414_powerdown(struct tcs3414_data *data)
  294. {
  295. return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
  296. data->control & ~(TCS3414_CONTROL_POWER |
  297. TCS3414_CONTROL_ADC_EN));
  298. }
  299. static int tcs3414_remove(struct i2c_client *client)
  300. {
  301. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  302. iio_device_unregister(indio_dev);
  303. iio_triggered_buffer_cleanup(indio_dev);
  304. tcs3414_powerdown(iio_priv(indio_dev));
  305. return 0;
  306. }
  307. #ifdef CONFIG_PM_SLEEP
  308. static int tcs3414_suspend(struct device *dev)
  309. {
  310. struct tcs3414_data *data = iio_priv(i2c_get_clientdata(
  311. to_i2c_client(dev)));
  312. return tcs3414_powerdown(data);
  313. }
  314. static int tcs3414_resume(struct device *dev)
  315. {
  316. struct tcs3414_data *data = iio_priv(i2c_get_clientdata(
  317. to_i2c_client(dev)));
  318. return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
  319. data->control);
  320. }
  321. #endif
  322. static SIMPLE_DEV_PM_OPS(tcs3414_pm_ops, tcs3414_suspend, tcs3414_resume);
  323. static const struct i2c_device_id tcs3414_id[] = {
  324. { "tcs3414", 0 },
  325. { }
  326. };
  327. MODULE_DEVICE_TABLE(i2c, tcs3414_id);
  328. static struct i2c_driver tcs3414_driver = {
  329. .driver = {
  330. .name = TCS3414_DRV_NAME,
  331. .pm = &tcs3414_pm_ops,
  332. },
  333. .probe = tcs3414_probe,
  334. .remove = tcs3414_remove,
  335. .id_table = tcs3414_id,
  336. };
  337. module_i2c_driver(tcs3414_driver);
  338. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  339. MODULE_DESCRIPTION("TCS3414 digital color sensors driver");
  340. MODULE_LICENSE("GPL");