adjd_s311.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * adjd_s311.c - Support for ADJD-S311-CR999 digital color sensor
  3. *
  4. * Copyright (C) 2012 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. * driver for ADJD-S311-CR999 digital color sensor (10-bit channels for
  11. * red, green, blue, clear); 7-bit I2C slave address 0x74
  12. *
  13. * limitations: no calibration, no offset mode, no sleep mode
  14. */
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/i2c.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/bitmap.h>
  21. #include <linux/err.h>
  22. #include <linux/irq.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/sysfs.h>
  25. #include <linux/iio/trigger_consumer.h>
  26. #include <linux/iio/buffer.h>
  27. #include <linux/iio/triggered_buffer.h>
  28. #define ADJD_S311_DRV_NAME "adjd_s311"
  29. #define ADJD_S311_CTRL 0x00
  30. #define ADJD_S311_CONFIG 0x01
  31. #define ADJD_S311_CAP_RED 0x06
  32. #define ADJD_S311_CAP_GREEN 0x07
  33. #define ADJD_S311_CAP_BLUE 0x08
  34. #define ADJD_S311_CAP_CLEAR 0x09
  35. #define ADJD_S311_INT_RED 0x0a
  36. #define ADJD_S311_INT_GREEN 0x0c
  37. #define ADJD_S311_INT_BLUE 0x0e
  38. #define ADJD_S311_INT_CLEAR 0x10
  39. #define ADJD_S311_DATA_RED 0x40
  40. #define ADJD_S311_DATA_GREEN 0x42
  41. #define ADJD_S311_DATA_BLUE 0x44
  42. #define ADJD_S311_DATA_CLEAR 0x46
  43. #define ADJD_S311_OFFSET_RED 0x48
  44. #define ADJD_S311_OFFSET_GREEN 0x49
  45. #define ADJD_S311_OFFSET_BLUE 0x4a
  46. #define ADJD_S311_OFFSET_CLEAR 0x4b
  47. #define ADJD_S311_CTRL_GOFS 0x02
  48. #define ADJD_S311_CTRL_GSSR 0x01
  49. #define ADJD_S311_CAP_MASK 0x0f
  50. #define ADJD_S311_INT_MASK 0x0fff
  51. #define ADJD_S311_DATA_MASK 0x03ff
  52. struct adjd_s311_data {
  53. struct i2c_client *client;
  54. u16 *buffer;
  55. };
  56. enum adjd_s311_channel_idx {
  57. IDX_RED, IDX_GREEN, IDX_BLUE, IDX_CLEAR
  58. };
  59. #define ADJD_S311_DATA_REG(chan) (ADJD_S311_DATA_RED + (chan) * 2)
  60. #define ADJD_S311_INT_REG(chan) (ADJD_S311_INT_RED + (chan) * 2)
  61. #define ADJD_S311_CAP_REG(chan) (ADJD_S311_CAP_RED + (chan))
  62. static int adjd_s311_req_data(struct iio_dev *indio_dev)
  63. {
  64. struct adjd_s311_data *data = iio_priv(indio_dev);
  65. int tries = 10;
  66. int ret = i2c_smbus_write_byte_data(data->client, ADJD_S311_CTRL,
  67. ADJD_S311_CTRL_GSSR);
  68. if (ret < 0)
  69. return ret;
  70. while (tries--) {
  71. ret = i2c_smbus_read_byte_data(data->client, ADJD_S311_CTRL);
  72. if (ret < 0)
  73. return ret;
  74. if (!(ret & ADJD_S311_CTRL_GSSR))
  75. break;
  76. msleep(20);
  77. }
  78. if (tries < 0) {
  79. dev_err(&data->client->dev,
  80. "adjd_s311_req_data() failed, data not ready\n");
  81. return -EIO;
  82. }
  83. return 0;
  84. }
  85. static int adjd_s311_read_data(struct iio_dev *indio_dev, u8 reg, int *val)
  86. {
  87. struct adjd_s311_data *data = iio_priv(indio_dev);
  88. int ret = adjd_s311_req_data(indio_dev);
  89. if (ret < 0)
  90. return ret;
  91. ret = i2c_smbus_read_word_data(data->client, reg);
  92. if (ret < 0)
  93. return ret;
  94. *val = ret & ADJD_S311_DATA_MASK;
  95. return 0;
  96. }
  97. static irqreturn_t adjd_s311_trigger_handler(int irq, void *p)
  98. {
  99. struct iio_poll_func *pf = p;
  100. struct iio_dev *indio_dev = pf->indio_dev;
  101. struct adjd_s311_data *data = iio_priv(indio_dev);
  102. s64 time_ns = iio_get_time_ns(indio_dev);
  103. int i, j = 0;
  104. int ret = adjd_s311_req_data(indio_dev);
  105. if (ret < 0)
  106. goto done;
  107. for_each_set_bit(i, indio_dev->active_scan_mask,
  108. indio_dev->masklength) {
  109. ret = i2c_smbus_read_word_data(data->client,
  110. ADJD_S311_DATA_REG(i));
  111. if (ret < 0)
  112. goto done;
  113. data->buffer[j++] = ret & ADJD_S311_DATA_MASK;
  114. }
  115. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, time_ns);
  116. done:
  117. iio_trigger_notify_done(indio_dev->trig);
  118. return IRQ_HANDLED;
  119. }
  120. #define ADJD_S311_CHANNEL(_color, _scan_idx) { \
  121. .type = IIO_INTENSITY, \
  122. .modified = 1, \
  123. .address = (IDX_##_color), \
  124. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  125. BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
  126. BIT(IIO_CHAN_INFO_INT_TIME), \
  127. .channel2 = (IIO_MOD_LIGHT_##_color), \
  128. .scan_index = (_scan_idx), \
  129. .scan_type = { \
  130. .sign = 'u', \
  131. .realbits = 10, \
  132. .storagebits = 16, \
  133. .endianness = IIO_CPU, \
  134. }, \
  135. }
  136. static const struct iio_chan_spec adjd_s311_channels[] = {
  137. ADJD_S311_CHANNEL(RED, 0),
  138. ADJD_S311_CHANNEL(GREEN, 1),
  139. ADJD_S311_CHANNEL(BLUE, 2),
  140. ADJD_S311_CHANNEL(CLEAR, 3),
  141. IIO_CHAN_SOFT_TIMESTAMP(4),
  142. };
  143. static int adjd_s311_read_raw(struct iio_dev *indio_dev,
  144. struct iio_chan_spec const *chan,
  145. int *val, int *val2, long mask)
  146. {
  147. struct adjd_s311_data *data = iio_priv(indio_dev);
  148. int ret;
  149. switch (mask) {
  150. case IIO_CHAN_INFO_RAW:
  151. ret = adjd_s311_read_data(indio_dev,
  152. ADJD_S311_DATA_REG(chan->address), val);
  153. if (ret < 0)
  154. return ret;
  155. return IIO_VAL_INT;
  156. case IIO_CHAN_INFO_HARDWAREGAIN:
  157. ret = i2c_smbus_read_byte_data(data->client,
  158. ADJD_S311_CAP_REG(chan->address));
  159. if (ret < 0)
  160. return ret;
  161. *val = ret & ADJD_S311_CAP_MASK;
  162. return IIO_VAL_INT;
  163. case IIO_CHAN_INFO_INT_TIME:
  164. ret = i2c_smbus_read_word_data(data->client,
  165. ADJD_S311_INT_REG(chan->address));
  166. if (ret < 0)
  167. return ret;
  168. *val = 0;
  169. /*
  170. * not documented, based on measurement:
  171. * 4095 LSBs correspond to roughly 4 ms
  172. */
  173. *val2 = ret & ADJD_S311_INT_MASK;
  174. return IIO_VAL_INT_PLUS_MICRO;
  175. }
  176. return -EINVAL;
  177. }
  178. static int adjd_s311_write_raw(struct iio_dev *indio_dev,
  179. struct iio_chan_spec const *chan,
  180. int val, int val2, long mask)
  181. {
  182. struct adjd_s311_data *data = iio_priv(indio_dev);
  183. switch (mask) {
  184. case IIO_CHAN_INFO_HARDWAREGAIN:
  185. if (val < 0 || val > ADJD_S311_CAP_MASK)
  186. return -EINVAL;
  187. return i2c_smbus_write_byte_data(data->client,
  188. ADJD_S311_CAP_REG(chan->address), val);
  189. case IIO_CHAN_INFO_INT_TIME:
  190. if (val != 0 || val2 < 0 || val2 > ADJD_S311_INT_MASK)
  191. return -EINVAL;
  192. return i2c_smbus_write_word_data(data->client,
  193. ADJD_S311_INT_REG(chan->address), val2);
  194. }
  195. return -EINVAL;
  196. }
  197. static int adjd_s311_update_scan_mode(struct iio_dev *indio_dev,
  198. const unsigned long *scan_mask)
  199. {
  200. struct adjd_s311_data *data = iio_priv(indio_dev);
  201. kfree(data->buffer);
  202. data->buffer = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
  203. if (data->buffer == NULL)
  204. return -ENOMEM;
  205. return 0;
  206. }
  207. static const struct iio_info adjd_s311_info = {
  208. .read_raw = adjd_s311_read_raw,
  209. .write_raw = adjd_s311_write_raw,
  210. .update_scan_mode = adjd_s311_update_scan_mode,
  211. .driver_module = THIS_MODULE,
  212. };
  213. static int adjd_s311_probe(struct i2c_client *client,
  214. const struct i2c_device_id *id)
  215. {
  216. struct adjd_s311_data *data;
  217. struct iio_dev *indio_dev;
  218. int err;
  219. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  220. if (indio_dev == NULL)
  221. return -ENOMEM;
  222. data = iio_priv(indio_dev);
  223. i2c_set_clientdata(client, indio_dev);
  224. data->client = client;
  225. indio_dev->dev.parent = &client->dev;
  226. indio_dev->info = &adjd_s311_info;
  227. indio_dev->name = ADJD_S311_DRV_NAME;
  228. indio_dev->channels = adjd_s311_channels;
  229. indio_dev->num_channels = ARRAY_SIZE(adjd_s311_channels);
  230. indio_dev->modes = INDIO_DIRECT_MODE;
  231. err = iio_triggered_buffer_setup(indio_dev, NULL,
  232. adjd_s311_trigger_handler, NULL);
  233. if (err < 0)
  234. return err;
  235. err = iio_device_register(indio_dev);
  236. if (err)
  237. goto exit_unreg_buffer;
  238. dev_info(&client->dev, "ADJD-S311 color sensor registered\n");
  239. return 0;
  240. exit_unreg_buffer:
  241. iio_triggered_buffer_cleanup(indio_dev);
  242. return err;
  243. }
  244. static int adjd_s311_remove(struct i2c_client *client)
  245. {
  246. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  247. struct adjd_s311_data *data = iio_priv(indio_dev);
  248. iio_device_unregister(indio_dev);
  249. iio_triggered_buffer_cleanup(indio_dev);
  250. kfree(data->buffer);
  251. return 0;
  252. }
  253. static const struct i2c_device_id adjd_s311_id[] = {
  254. { "adjd_s311", 0 },
  255. { }
  256. };
  257. MODULE_DEVICE_TABLE(i2c, adjd_s311_id);
  258. static struct i2c_driver adjd_s311_driver = {
  259. .driver = {
  260. .name = ADJD_S311_DRV_NAME,
  261. },
  262. .probe = adjd_s311_probe,
  263. .remove = adjd_s311_remove,
  264. .id_table = adjd_s311_id,
  265. };
  266. module_i2c_driver(adjd_s311_driver);
  267. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  268. MODULE_DESCRIPTION("ADJD-S311 color sensor");
  269. MODULE_LICENSE("GPL");