cm32181.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (C) 2013 Capella Microsystems Inc.
  3. * Author: Kevin Tsai <ktsai@capellamicro.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2, as published
  7. * by the Free Software Foundation.
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/i2c.h>
  12. #include <linux/mutex.h>
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #include <linux/iio/events.h>
  19. #include <linux/init.h>
  20. /* Registers Address */
  21. #define CM32181_REG_ADDR_CMD 0x00
  22. #define CM32181_REG_ADDR_ALS 0x04
  23. #define CM32181_REG_ADDR_STATUS 0x06
  24. #define CM32181_REG_ADDR_ID 0x07
  25. /* Number of Configurable Registers */
  26. #define CM32181_CONF_REG_NUM 0x01
  27. /* CMD register */
  28. #define CM32181_CMD_ALS_ENABLE 0x00
  29. #define CM32181_CMD_ALS_DISABLE 0x01
  30. #define CM32181_CMD_ALS_INT_EN 0x02
  31. #define CM32181_CMD_ALS_IT_SHIFT 6
  32. #define CM32181_CMD_ALS_IT_MASK (0x0F << CM32181_CMD_ALS_IT_SHIFT)
  33. #define CM32181_CMD_ALS_IT_DEFAULT (0x00 << CM32181_CMD_ALS_IT_SHIFT)
  34. #define CM32181_CMD_ALS_SM_SHIFT 11
  35. #define CM32181_CMD_ALS_SM_MASK (0x03 << CM32181_CMD_ALS_SM_SHIFT)
  36. #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT)
  37. #define CM32181_MLUX_PER_BIT 5 /* ALS_SM=01 IT=800ms */
  38. #define CM32181_MLUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */
  39. #define CM32181_CALIBSCALE_DEFAULT 1000
  40. #define CM32181_CALIBSCALE_RESOLUTION 1000
  41. #define MLUX_PER_LUX 1000
  42. static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
  43. CM32181_REG_ADDR_CMD,
  44. };
  45. static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
  46. static const int als_it_value[] = {25000, 50000, 100000, 200000, 400000,
  47. 800000};
  48. struct cm32181_chip {
  49. struct i2c_client *client;
  50. struct mutex lock;
  51. u16 conf_regs[CM32181_CONF_REG_NUM];
  52. int calibscale;
  53. };
  54. /**
  55. * cm32181_reg_init() - Initialize CM32181 registers
  56. * @cm32181: pointer of struct cm32181.
  57. *
  58. * Initialize CM32181 ambient light sensor register to default values.
  59. *
  60. * Return: 0 for success; otherwise for error code.
  61. */
  62. static int cm32181_reg_init(struct cm32181_chip *cm32181)
  63. {
  64. struct i2c_client *client = cm32181->client;
  65. int i;
  66. s32 ret;
  67. ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID);
  68. if (ret < 0)
  69. return ret;
  70. /* check device ID */
  71. if ((ret & 0xFF) != 0x81)
  72. return -ENODEV;
  73. /* Default Values */
  74. cm32181->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_ALS_ENABLE |
  75. CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT;
  76. cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT;
  77. /* Initialize registers*/
  78. for (i = 0; i < CM32181_CONF_REG_NUM; i++) {
  79. ret = i2c_smbus_write_word_data(client, cm32181_reg[i],
  80. cm32181->conf_regs[i]);
  81. if (ret < 0)
  82. return ret;
  83. }
  84. return 0;
  85. }
  86. /**
  87. * cm32181_read_als_it() - Get sensor integration time (ms)
  88. * @cm32181: pointer of struct cm32181
  89. * @val2: pointer of int to load the als_it value.
  90. *
  91. * Report the current integartion time by millisecond.
  92. *
  93. * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
  94. */
  95. static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2)
  96. {
  97. u16 als_it;
  98. int i;
  99. als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD];
  100. als_it &= CM32181_CMD_ALS_IT_MASK;
  101. als_it >>= CM32181_CMD_ALS_IT_SHIFT;
  102. for (i = 0; i < ARRAY_SIZE(als_it_bits); i++) {
  103. if (als_it == als_it_bits[i]) {
  104. *val2 = als_it_value[i];
  105. return IIO_VAL_INT_PLUS_MICRO;
  106. }
  107. }
  108. return -EINVAL;
  109. }
  110. /**
  111. * cm32181_write_als_it() - Write sensor integration time
  112. * @cm32181: pointer of struct cm32181.
  113. * @val: integration time by millisecond.
  114. *
  115. * Convert integration time (ms) to sensor value.
  116. *
  117. * Return: i2c_smbus_write_word_data command return value.
  118. */
  119. static int cm32181_write_als_it(struct cm32181_chip *cm32181, int val)
  120. {
  121. struct i2c_client *client = cm32181->client;
  122. u16 als_it;
  123. int ret, i, n;
  124. n = ARRAY_SIZE(als_it_value);
  125. for (i = 0; i < n; i++)
  126. if (val <= als_it_value[i])
  127. break;
  128. if (i >= n)
  129. i = n - 1;
  130. als_it = als_it_bits[i];
  131. als_it <<= CM32181_CMD_ALS_IT_SHIFT;
  132. mutex_lock(&cm32181->lock);
  133. cm32181->conf_regs[CM32181_REG_ADDR_CMD] &=
  134. ~CM32181_CMD_ALS_IT_MASK;
  135. cm32181->conf_regs[CM32181_REG_ADDR_CMD] |=
  136. als_it;
  137. ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
  138. cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
  139. mutex_unlock(&cm32181->lock);
  140. return ret;
  141. }
  142. /**
  143. * cm32181_get_lux() - report current lux value
  144. * @cm32181: pointer of struct cm32181.
  145. *
  146. * Convert sensor raw data to lux. It depends on integration
  147. * time and calibscale variable.
  148. *
  149. * Return: Positive value is lux, otherwise is error code.
  150. */
  151. static int cm32181_get_lux(struct cm32181_chip *cm32181)
  152. {
  153. struct i2c_client *client = cm32181->client;
  154. int ret;
  155. int als_it;
  156. unsigned long lux;
  157. ret = cm32181_read_als_it(cm32181, &als_it);
  158. if (ret < 0)
  159. return -EINVAL;
  160. lux = CM32181_MLUX_PER_BIT;
  161. lux *= CM32181_MLUX_PER_BIT_BASE_IT;
  162. lux /= als_it;
  163. ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
  164. if (ret < 0)
  165. return ret;
  166. lux *= ret;
  167. lux *= cm32181->calibscale;
  168. lux /= CM32181_CALIBSCALE_RESOLUTION;
  169. lux /= MLUX_PER_LUX;
  170. if (lux > 0xFFFF)
  171. lux = 0xFFFF;
  172. return lux;
  173. }
  174. static int cm32181_read_raw(struct iio_dev *indio_dev,
  175. struct iio_chan_spec const *chan,
  176. int *val, int *val2, long mask)
  177. {
  178. struct cm32181_chip *cm32181 = iio_priv(indio_dev);
  179. int ret;
  180. switch (mask) {
  181. case IIO_CHAN_INFO_PROCESSED:
  182. ret = cm32181_get_lux(cm32181);
  183. if (ret < 0)
  184. return ret;
  185. *val = ret;
  186. return IIO_VAL_INT;
  187. case IIO_CHAN_INFO_CALIBSCALE:
  188. *val = cm32181->calibscale;
  189. return IIO_VAL_INT;
  190. case IIO_CHAN_INFO_INT_TIME:
  191. *val = 0;
  192. ret = cm32181_read_als_it(cm32181, val2);
  193. return ret;
  194. }
  195. return -EINVAL;
  196. }
  197. static int cm32181_write_raw(struct iio_dev *indio_dev,
  198. struct iio_chan_spec const *chan,
  199. int val, int val2, long mask)
  200. {
  201. struct cm32181_chip *cm32181 = iio_priv(indio_dev);
  202. int ret;
  203. switch (mask) {
  204. case IIO_CHAN_INFO_CALIBSCALE:
  205. cm32181->calibscale = val;
  206. return val;
  207. case IIO_CHAN_INFO_INT_TIME:
  208. ret = cm32181_write_als_it(cm32181, val2);
  209. return ret;
  210. }
  211. return -EINVAL;
  212. }
  213. /**
  214. * cm32181_get_it_available() - Get available ALS IT value
  215. * @dev: pointer of struct device.
  216. * @attr: pointer of struct device_attribute.
  217. * @buf: pointer of return string buffer.
  218. *
  219. * Display the available integration time values by millisecond.
  220. *
  221. * Return: string length.
  222. */
  223. static ssize_t cm32181_get_it_available(struct device *dev,
  224. struct device_attribute *attr, char *buf)
  225. {
  226. int i, n, len;
  227. n = ARRAY_SIZE(als_it_value);
  228. for (i = 0, len = 0; i < n; i++)
  229. len += sprintf(buf + len, "0.%06u ", als_it_value[i]);
  230. return len + sprintf(buf + len, "\n");
  231. }
  232. static const struct iio_chan_spec cm32181_channels[] = {
  233. {
  234. .type = IIO_LIGHT,
  235. .info_mask_separate =
  236. BIT(IIO_CHAN_INFO_PROCESSED) |
  237. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  238. BIT(IIO_CHAN_INFO_INT_TIME),
  239. }
  240. };
  241. static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
  242. S_IRUGO, cm32181_get_it_available, NULL, 0);
  243. static struct attribute *cm32181_attributes[] = {
  244. &iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
  245. NULL,
  246. };
  247. static const struct attribute_group cm32181_attribute_group = {
  248. .attrs = cm32181_attributes
  249. };
  250. static const struct iio_info cm32181_info = {
  251. .driver_module = THIS_MODULE,
  252. .read_raw = &cm32181_read_raw,
  253. .write_raw = &cm32181_write_raw,
  254. .attrs = &cm32181_attribute_group,
  255. };
  256. static int cm32181_probe(struct i2c_client *client,
  257. const struct i2c_device_id *id)
  258. {
  259. struct cm32181_chip *cm32181;
  260. struct iio_dev *indio_dev;
  261. int ret;
  262. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm32181));
  263. if (!indio_dev) {
  264. dev_err(&client->dev, "devm_iio_device_alloc failed\n");
  265. return -ENOMEM;
  266. }
  267. cm32181 = iio_priv(indio_dev);
  268. i2c_set_clientdata(client, indio_dev);
  269. cm32181->client = client;
  270. mutex_init(&cm32181->lock);
  271. indio_dev->dev.parent = &client->dev;
  272. indio_dev->channels = cm32181_channels;
  273. indio_dev->num_channels = ARRAY_SIZE(cm32181_channels);
  274. indio_dev->info = &cm32181_info;
  275. indio_dev->name = id->name;
  276. indio_dev->modes = INDIO_DIRECT_MODE;
  277. ret = cm32181_reg_init(cm32181);
  278. if (ret) {
  279. dev_err(&client->dev,
  280. "%s: register init failed\n",
  281. __func__);
  282. return ret;
  283. }
  284. ret = devm_iio_device_register(&client->dev, indio_dev);
  285. if (ret) {
  286. dev_err(&client->dev,
  287. "%s: regist device failed\n",
  288. __func__);
  289. return ret;
  290. }
  291. return 0;
  292. }
  293. static const struct i2c_device_id cm32181_id[] = {
  294. { "cm32181", 0 },
  295. { }
  296. };
  297. MODULE_DEVICE_TABLE(i2c, cm32181_id);
  298. static const struct of_device_id cm32181_of_match[] = {
  299. { .compatible = "capella,cm32181" },
  300. { }
  301. };
  302. MODULE_DEVICE_TABLE(of, cm32181_of_match);
  303. static struct i2c_driver cm32181_driver = {
  304. .driver = {
  305. .name = "cm32181",
  306. .of_match_table = of_match_ptr(cm32181_of_match),
  307. },
  308. .id_table = cm32181_id,
  309. .probe = cm32181_probe,
  310. };
  311. module_i2c_driver(cm32181_driver);
  312. MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
  313. MODULE_DESCRIPTION("CM32181 ambient light sensor driver");
  314. MODULE_LICENSE("GPL");