tsl4531.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * tsl4531.c - Support for TAOS TSL4531 ambient light sensor
  3. *
  4. * Copyright 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. * IIO driver for the TSL4531x family
  11. * TSL45311/TSL45313: 7-bit I2C slave address 0x39
  12. * TSL45315/TSL45317: 7-bit I2C slave address 0x29
  13. *
  14. * TODO: single cycle measurement
  15. */
  16. #include <linux/module.h>
  17. #include <linux/i2c.h>
  18. #include <linux/err.h>
  19. #include <linux/delay.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/sysfs.h>
  22. #define TSL4531_DRV_NAME "tsl4531"
  23. #define TSL4531_COMMAND BIT(7)
  24. #define TSL4531_CONTROL (TSL4531_COMMAND | 0x00)
  25. #define TSL4531_CONFIG (TSL4531_COMMAND | 0x01)
  26. #define TSL4531_DATA (TSL4531_COMMAND | 0x04)
  27. #define TSL4531_ID (TSL4531_COMMAND | 0x0a)
  28. /* operating modes in control register */
  29. #define TSL4531_MODE_POWERDOWN 0x00
  30. #define TSL4531_MODE_SINGLE_ADC 0x02
  31. #define TSL4531_MODE_NORMAL 0x03
  32. /* integration time control in config register */
  33. #define TSL4531_TCNTRL_400MS 0x00
  34. #define TSL4531_TCNTRL_200MS 0x01
  35. #define TSL4531_TCNTRL_100MS 0x02
  36. /* part number in id register */
  37. #define TSL45311_ID 0x8
  38. #define TSL45313_ID 0x9
  39. #define TSL45315_ID 0xa
  40. #define TSL45317_ID 0xb
  41. #define TSL4531_ID_SHIFT 4
  42. struct tsl4531_data {
  43. struct i2c_client *client;
  44. struct mutex lock;
  45. int int_time;
  46. };
  47. static IIO_CONST_ATTR_INT_TIME_AVAIL("0.1 0.2 0.4");
  48. static struct attribute *tsl4531_attributes[] = {
  49. &iio_const_attr_integration_time_available.dev_attr.attr,
  50. NULL
  51. };
  52. static const struct attribute_group tsl4531_attribute_group = {
  53. .attrs = tsl4531_attributes,
  54. };
  55. static const struct iio_chan_spec tsl4531_channels[] = {
  56. {
  57. .type = IIO_LIGHT,
  58. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  59. BIT(IIO_CHAN_INFO_SCALE) |
  60. BIT(IIO_CHAN_INFO_INT_TIME)
  61. }
  62. };
  63. static int tsl4531_read_raw(struct iio_dev *indio_dev,
  64. struct iio_chan_spec const *chan,
  65. int *val, int *val2, long mask)
  66. {
  67. struct tsl4531_data *data = iio_priv(indio_dev);
  68. int ret;
  69. switch (mask) {
  70. case IIO_CHAN_INFO_RAW:
  71. ret = i2c_smbus_read_word_data(data->client,
  72. TSL4531_DATA);
  73. if (ret < 0)
  74. return ret;
  75. *val = ret;
  76. return IIO_VAL_INT;
  77. case IIO_CHAN_INFO_SCALE:
  78. /* 0.. 1x, 1 .. 2x, 2 .. 4x */
  79. *val = 1 << data->int_time;
  80. return IIO_VAL_INT;
  81. case IIO_CHAN_INFO_INT_TIME:
  82. if (data->int_time == 0)
  83. *val2 = 400000;
  84. else if (data->int_time == 1)
  85. *val2 = 200000;
  86. else if (data->int_time == 2)
  87. *val2 = 100000;
  88. else
  89. return -EINVAL;
  90. *val = 0;
  91. return IIO_VAL_INT_PLUS_MICRO;
  92. default:
  93. return -EINVAL;
  94. }
  95. }
  96. static int tsl4531_write_raw(struct iio_dev *indio_dev,
  97. struct iio_chan_spec const *chan,
  98. int val, int val2, long mask)
  99. {
  100. struct tsl4531_data *data = iio_priv(indio_dev);
  101. int int_time, ret;
  102. switch (mask) {
  103. case IIO_CHAN_INFO_INT_TIME:
  104. if (val != 0)
  105. return -EINVAL;
  106. if (val2 == 400000)
  107. int_time = 0;
  108. else if (val2 == 200000)
  109. int_time = 1;
  110. else if (val2 == 100000)
  111. int_time = 2;
  112. else
  113. return -EINVAL;
  114. mutex_lock(&data->lock);
  115. ret = i2c_smbus_write_byte_data(data->client,
  116. TSL4531_CONFIG, int_time);
  117. if (ret >= 0)
  118. data->int_time = int_time;
  119. mutex_unlock(&data->lock);
  120. return ret;
  121. default:
  122. return -EINVAL;
  123. }
  124. }
  125. static const struct iio_info tsl4531_info = {
  126. .read_raw = tsl4531_read_raw,
  127. .write_raw = tsl4531_write_raw,
  128. .attrs = &tsl4531_attribute_group,
  129. };
  130. static int tsl4531_check_id(struct i2c_client *client)
  131. {
  132. int ret = i2c_smbus_read_byte_data(client, TSL4531_ID);
  133. if (ret < 0)
  134. return ret;
  135. switch (ret >> TSL4531_ID_SHIFT) {
  136. case TSL45311_ID:
  137. case TSL45313_ID:
  138. case TSL45315_ID:
  139. case TSL45317_ID:
  140. return 0;
  141. default:
  142. return -ENODEV;
  143. }
  144. }
  145. static int tsl4531_probe(struct i2c_client *client,
  146. const struct i2c_device_id *id)
  147. {
  148. struct tsl4531_data *data;
  149. struct iio_dev *indio_dev;
  150. int ret;
  151. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  152. if (!indio_dev)
  153. return -ENOMEM;
  154. data = iio_priv(indio_dev);
  155. i2c_set_clientdata(client, indio_dev);
  156. data->client = client;
  157. mutex_init(&data->lock);
  158. ret = tsl4531_check_id(client);
  159. if (ret) {
  160. dev_err(&client->dev, "no TSL4531 sensor\n");
  161. return ret;
  162. }
  163. ret = i2c_smbus_write_byte_data(data->client, TSL4531_CONTROL,
  164. TSL4531_MODE_NORMAL);
  165. if (ret < 0)
  166. return ret;
  167. ret = i2c_smbus_write_byte_data(data->client, TSL4531_CONFIG,
  168. TSL4531_TCNTRL_400MS);
  169. if (ret < 0)
  170. return ret;
  171. indio_dev->dev.parent = &client->dev;
  172. indio_dev->info = &tsl4531_info;
  173. indio_dev->channels = tsl4531_channels;
  174. indio_dev->num_channels = ARRAY_SIZE(tsl4531_channels);
  175. indio_dev->name = TSL4531_DRV_NAME;
  176. indio_dev->modes = INDIO_DIRECT_MODE;
  177. return iio_device_register(indio_dev);
  178. }
  179. static int tsl4531_powerdown(struct i2c_client *client)
  180. {
  181. return i2c_smbus_write_byte_data(client, TSL4531_CONTROL,
  182. TSL4531_MODE_POWERDOWN);
  183. }
  184. static int tsl4531_remove(struct i2c_client *client)
  185. {
  186. iio_device_unregister(i2c_get_clientdata(client));
  187. tsl4531_powerdown(client);
  188. return 0;
  189. }
  190. #ifdef CONFIG_PM_SLEEP
  191. static int tsl4531_suspend(struct device *dev)
  192. {
  193. return tsl4531_powerdown(to_i2c_client(dev));
  194. }
  195. static int tsl4531_resume(struct device *dev)
  196. {
  197. return i2c_smbus_write_byte_data(to_i2c_client(dev), TSL4531_CONTROL,
  198. TSL4531_MODE_NORMAL);
  199. }
  200. static SIMPLE_DEV_PM_OPS(tsl4531_pm_ops, tsl4531_suspend, tsl4531_resume);
  201. #define TSL4531_PM_OPS (&tsl4531_pm_ops)
  202. #else
  203. #define TSL4531_PM_OPS NULL
  204. #endif
  205. static const struct i2c_device_id tsl4531_id[] = {
  206. { "tsl4531", 0 },
  207. { }
  208. };
  209. MODULE_DEVICE_TABLE(i2c, tsl4531_id);
  210. static struct i2c_driver tsl4531_driver = {
  211. .driver = {
  212. .name = TSL4531_DRV_NAME,
  213. .pm = TSL4531_PM_OPS,
  214. },
  215. .probe = tsl4531_probe,
  216. .remove = tsl4531_remove,
  217. .id_table = tsl4531_id,
  218. };
  219. module_i2c_driver(tsl4531_driver);
  220. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  221. MODULE_DESCRIPTION("TAOS TSL4531 ambient light sensors driver");
  222. MODULE_LICENSE("GPL");