tcs3414.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. };
  212. static int tcs3414_buffer_preenable(struct iio_dev *indio_dev)
  213. {
  214. struct tcs3414_data *data = iio_priv(indio_dev);
  215. data->control |= TCS3414_CONTROL_ADC_EN;
  216. return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
  217. data->control);
  218. }
  219. static int tcs3414_buffer_predisable(struct iio_dev *indio_dev)
  220. {
  221. struct tcs3414_data *data = iio_priv(indio_dev);
  222. int ret;
  223. ret = iio_triggered_buffer_predisable(indio_dev);
  224. if (ret < 0)
  225. return ret;
  226. data->control &= ~TCS3414_CONTROL_ADC_EN;
  227. return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
  228. data->control);
  229. }
  230. static const struct iio_buffer_setup_ops tcs3414_buffer_setup_ops = {
  231. .preenable = tcs3414_buffer_preenable,
  232. .postenable = &iio_triggered_buffer_postenable,
  233. .predisable = tcs3414_buffer_predisable,
  234. };
  235. static int tcs3414_probe(struct i2c_client *client,
  236. const struct i2c_device_id *id)
  237. {
  238. struct tcs3414_data *data;
  239. struct iio_dev *indio_dev;
  240. int ret;
  241. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  242. if (indio_dev == NULL)
  243. return -ENOMEM;
  244. data = iio_priv(indio_dev);
  245. i2c_set_clientdata(client, indio_dev);
  246. data->client = client;
  247. indio_dev->dev.parent = &client->dev;
  248. indio_dev->info = &tcs3414_info;
  249. indio_dev->name = TCS3414_DRV_NAME;
  250. indio_dev->channels = tcs3414_channels;
  251. indio_dev->num_channels = ARRAY_SIZE(tcs3414_channels);
  252. indio_dev->modes = INDIO_DIRECT_MODE;
  253. ret = i2c_smbus_read_byte_data(data->client, TCS3414_ID);
  254. if (ret < 0)
  255. return ret;
  256. switch (ret & 0xf0) {
  257. case 0x00:
  258. dev_info(&client->dev, "TCS3404 found\n");
  259. break;
  260. case 0x10:
  261. dev_info(&client->dev, "TCS3413/14/15/16 found\n");
  262. break;
  263. default:
  264. return -ENODEV;
  265. }
  266. data->control = TCS3414_CONTROL_POWER;
  267. ret = i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
  268. data->control);
  269. if (ret < 0)
  270. return ret;
  271. data->timing = TCS3414_INTEG_12MS; /* free running */
  272. ret = i2c_smbus_write_byte_data(data->client, TCS3414_TIMING,
  273. data->timing);
  274. if (ret < 0)
  275. return ret;
  276. ret = i2c_smbus_read_byte_data(data->client, TCS3414_GAIN);
  277. if (ret < 0)
  278. return ret;
  279. data->gain = ret;
  280. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  281. tcs3414_trigger_handler, &tcs3414_buffer_setup_ops);
  282. if (ret < 0)
  283. return ret;
  284. ret = iio_device_register(indio_dev);
  285. if (ret < 0)
  286. goto buffer_cleanup;
  287. return 0;
  288. buffer_cleanup:
  289. iio_triggered_buffer_cleanup(indio_dev);
  290. return ret;
  291. }
  292. static int tcs3414_powerdown(struct tcs3414_data *data)
  293. {
  294. return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
  295. data->control & ~(TCS3414_CONTROL_POWER |
  296. TCS3414_CONTROL_ADC_EN));
  297. }
  298. static int tcs3414_remove(struct i2c_client *client)
  299. {
  300. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  301. iio_device_unregister(indio_dev);
  302. iio_triggered_buffer_cleanup(indio_dev);
  303. tcs3414_powerdown(iio_priv(indio_dev));
  304. return 0;
  305. }
  306. #ifdef CONFIG_PM_SLEEP
  307. static int tcs3414_suspend(struct device *dev)
  308. {
  309. struct tcs3414_data *data = iio_priv(i2c_get_clientdata(
  310. to_i2c_client(dev)));
  311. return tcs3414_powerdown(data);
  312. }
  313. static int tcs3414_resume(struct device *dev)
  314. {
  315. struct tcs3414_data *data = iio_priv(i2c_get_clientdata(
  316. to_i2c_client(dev)));
  317. return i2c_smbus_write_byte_data(data->client, TCS3414_CONTROL,
  318. data->control);
  319. }
  320. #endif
  321. static SIMPLE_DEV_PM_OPS(tcs3414_pm_ops, tcs3414_suspend, tcs3414_resume);
  322. static const struct i2c_device_id tcs3414_id[] = {
  323. { "tcs3414", 0 },
  324. { }
  325. };
  326. MODULE_DEVICE_TABLE(i2c, tcs3414_id);
  327. static struct i2c_driver tcs3414_driver = {
  328. .driver = {
  329. .name = TCS3414_DRV_NAME,
  330. .pm = &tcs3414_pm_ops,
  331. },
  332. .probe = tcs3414_probe,
  333. .remove = tcs3414_remove,
  334. .id_table = tcs3414_id,
  335. };
  336. module_i2c_driver(tcs3414_driver);
  337. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  338. MODULE_DESCRIPTION("TCS3414 digital color sensors driver");
  339. MODULE_LICENSE("GPL");