cm3323.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * CM3323 - Capella Color Light Sensor
  3. *
  4. * Copyright (c) 2015, Intel Corporation.
  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. * IIO driver for CM3323 (7-bit I2C slave address 0x10)
  11. *
  12. * TODO: calibscale to correct the lens factor
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/i2c.h>
  17. #include <linux/mutex.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #define CM3323_DRV_NAME "cm3323"
  21. #define CM3323_CMD_CONF 0x00
  22. #define CM3323_CMD_RED_DATA 0x08
  23. #define CM3323_CMD_GREEN_DATA 0x09
  24. #define CM3323_CMD_BLUE_DATA 0x0A
  25. #define CM3323_CMD_CLEAR_DATA 0x0B
  26. #define CM3323_CONF_SD_BIT BIT(0) /* sensor disable */
  27. #define CM3323_CONF_AF_BIT BIT(1) /* auto/manual force mode */
  28. #define CM3323_CONF_IT_MASK GENMASK(6, 4)
  29. #define CM3323_CONF_IT_SHIFT 4
  30. #define CM3323_INT_TIME_AVAILABLE "0.04 0.08 0.16 0.32 0.64 1.28"
  31. static const struct {
  32. int val;
  33. int val2;
  34. } cm3323_int_time[] = {
  35. {0, 40000}, /* 40 ms */
  36. {0, 80000}, /* 80 ms */
  37. {0, 160000}, /* 160 ms */
  38. {0, 320000}, /* 320 ms */
  39. {0, 640000}, /* 640 ms */
  40. {1, 280000}, /* 1280 ms */
  41. };
  42. struct cm3323_data {
  43. struct i2c_client *client;
  44. u16 reg_conf;
  45. struct mutex mutex;
  46. };
  47. #define CM3323_COLOR_CHANNEL(_color, _addr) { \
  48. .type = IIO_INTENSITY, \
  49. .modified = 1, \
  50. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  51. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), \
  52. .channel2 = IIO_MOD_LIGHT_##_color, \
  53. .address = _addr, \
  54. }
  55. static const struct iio_chan_spec cm3323_channels[] = {
  56. CM3323_COLOR_CHANNEL(RED, CM3323_CMD_RED_DATA),
  57. CM3323_COLOR_CHANNEL(GREEN, CM3323_CMD_GREEN_DATA),
  58. CM3323_COLOR_CHANNEL(BLUE, CM3323_CMD_BLUE_DATA),
  59. CM3323_COLOR_CHANNEL(CLEAR, CM3323_CMD_CLEAR_DATA),
  60. };
  61. static IIO_CONST_ATTR_INT_TIME_AVAIL(CM3323_INT_TIME_AVAILABLE);
  62. static struct attribute *cm3323_attributes[] = {
  63. &iio_const_attr_integration_time_available.dev_attr.attr,
  64. NULL
  65. };
  66. static const struct attribute_group cm3323_attribute_group = {
  67. .attrs = cm3323_attributes,
  68. };
  69. static int cm3323_init(struct iio_dev *indio_dev)
  70. {
  71. int ret;
  72. struct cm3323_data *data = iio_priv(indio_dev);
  73. ret = i2c_smbus_read_word_data(data->client, CM3323_CMD_CONF);
  74. if (ret < 0) {
  75. dev_err(&data->client->dev, "Error reading reg_conf\n");
  76. return ret;
  77. }
  78. /* enable sensor and set auto force mode */
  79. ret &= ~(CM3323_CONF_SD_BIT | CM3323_CONF_AF_BIT);
  80. ret = i2c_smbus_write_word_data(data->client, CM3323_CMD_CONF, ret);
  81. if (ret < 0) {
  82. dev_err(&data->client->dev, "Error writing reg_conf\n");
  83. return ret;
  84. }
  85. data->reg_conf = ret;
  86. return 0;
  87. }
  88. static void cm3323_disable(struct iio_dev *indio_dev)
  89. {
  90. int ret;
  91. struct cm3323_data *data = iio_priv(indio_dev);
  92. ret = i2c_smbus_write_word_data(data->client, CM3323_CMD_CONF,
  93. CM3323_CONF_SD_BIT);
  94. if (ret < 0)
  95. dev_err(&data->client->dev, "Error writing reg_conf\n");
  96. }
  97. static int cm3323_set_it_bits(struct cm3323_data *data, int val, int val2)
  98. {
  99. int i, ret;
  100. u16 reg_conf;
  101. for (i = 0; i < ARRAY_SIZE(cm3323_int_time); i++) {
  102. if (val == cm3323_int_time[i].val &&
  103. val2 == cm3323_int_time[i].val2) {
  104. reg_conf = data->reg_conf & ~CM3323_CONF_IT_MASK;
  105. reg_conf |= i << CM3323_CONF_IT_SHIFT;
  106. ret = i2c_smbus_write_word_data(data->client,
  107. CM3323_CMD_CONF,
  108. reg_conf);
  109. if (ret < 0)
  110. return ret;
  111. data->reg_conf = reg_conf;
  112. return 0;
  113. }
  114. }
  115. return -EINVAL;
  116. }
  117. static int cm3323_get_it_bits(struct cm3323_data *data)
  118. {
  119. int bits;
  120. bits = (data->reg_conf & CM3323_CONF_IT_MASK) >>
  121. CM3323_CONF_IT_SHIFT;
  122. if (bits >= ARRAY_SIZE(cm3323_int_time))
  123. return -EINVAL;
  124. return bits;
  125. }
  126. static int cm3323_read_raw(struct iio_dev *indio_dev,
  127. struct iio_chan_spec const *chan, int *val,
  128. int *val2, long mask)
  129. {
  130. int ret;
  131. struct cm3323_data *data = iio_priv(indio_dev);
  132. switch (mask) {
  133. case IIO_CHAN_INFO_RAW:
  134. mutex_lock(&data->mutex);
  135. ret = i2c_smbus_read_word_data(data->client, chan->address);
  136. if (ret < 0) {
  137. mutex_unlock(&data->mutex);
  138. return ret;
  139. }
  140. *val = ret;
  141. mutex_unlock(&data->mutex);
  142. return IIO_VAL_INT;
  143. case IIO_CHAN_INFO_INT_TIME:
  144. mutex_lock(&data->mutex);
  145. ret = cm3323_get_it_bits(data);
  146. if (ret < 0) {
  147. mutex_unlock(&data->mutex);
  148. return ret;
  149. }
  150. *val = cm3323_int_time[ret].val;
  151. *val2 = cm3323_int_time[ret].val2;
  152. mutex_unlock(&data->mutex);
  153. return IIO_VAL_INT_PLUS_MICRO;
  154. default:
  155. return -EINVAL;
  156. }
  157. }
  158. static int cm3323_write_raw(struct iio_dev *indio_dev,
  159. struct iio_chan_spec const *chan, int val,
  160. int val2, long mask)
  161. {
  162. struct cm3323_data *data = iio_priv(indio_dev);
  163. int ret;
  164. switch (mask) {
  165. case IIO_CHAN_INFO_INT_TIME:
  166. mutex_lock(&data->mutex);
  167. ret = cm3323_set_it_bits(data, val, val2);
  168. mutex_unlock(&data->mutex);
  169. return ret;
  170. default:
  171. return -EINVAL;
  172. }
  173. }
  174. static const struct iio_info cm3323_info = {
  175. .read_raw = cm3323_read_raw,
  176. .write_raw = cm3323_write_raw,
  177. .attrs = &cm3323_attribute_group,
  178. };
  179. static int cm3323_probe(struct i2c_client *client,
  180. const struct i2c_device_id *id)
  181. {
  182. struct cm3323_data *data;
  183. struct iio_dev *indio_dev;
  184. int ret;
  185. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  186. if (!indio_dev)
  187. return -ENOMEM;
  188. data = iio_priv(indio_dev);
  189. i2c_set_clientdata(client, indio_dev);
  190. data->client = client;
  191. mutex_init(&data->mutex);
  192. indio_dev->dev.parent = &client->dev;
  193. indio_dev->info = &cm3323_info;
  194. indio_dev->name = CM3323_DRV_NAME;
  195. indio_dev->channels = cm3323_channels;
  196. indio_dev->num_channels = ARRAY_SIZE(cm3323_channels);
  197. indio_dev->modes = INDIO_DIRECT_MODE;
  198. ret = cm3323_init(indio_dev);
  199. if (ret < 0) {
  200. dev_err(&client->dev, "cm3323 chip init failed\n");
  201. return ret;
  202. }
  203. ret = iio_device_register(indio_dev);
  204. if (ret < 0) {
  205. dev_err(&client->dev, "failed to register iio dev\n");
  206. goto err_init;
  207. }
  208. return 0;
  209. err_init:
  210. cm3323_disable(indio_dev);
  211. return ret;
  212. }
  213. static int cm3323_remove(struct i2c_client *client)
  214. {
  215. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  216. iio_device_unregister(indio_dev);
  217. cm3323_disable(indio_dev);
  218. return 0;
  219. }
  220. static const struct i2c_device_id cm3323_id[] = {
  221. {"cm3323", 0},
  222. {}
  223. };
  224. MODULE_DEVICE_TABLE(i2c, cm3323_id);
  225. static struct i2c_driver cm3323_driver = {
  226. .driver = {
  227. .name = CM3323_DRV_NAME,
  228. },
  229. .probe = cm3323_probe,
  230. .remove = cm3323_remove,
  231. .id_table = cm3323_id,
  232. };
  233. module_i2c_driver(cm3323_driver);
  234. MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
  235. MODULE_DESCRIPTION("Capella CM3323 Color Light Sensor driver");
  236. MODULE_LICENSE("GPL v2");