cm3232.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * CM3232 Ambient Light Sensor
  3. *
  4. * Copyright (C) 2014-2015 Capella Microsystems Inc.
  5. * Author: Kevin Tsai <ktsai@capellamicro.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2, as published
  9. * by the Free Software Foundation.
  10. *
  11. * IIO driver for CM3232 (7-bit I2C slave address 0x10).
  12. */
  13. #include <linux/i2c.h>
  14. #include <linux/module.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/sysfs.h>
  17. #include <linux/init.h>
  18. /* Registers Address */
  19. #define CM3232_REG_ADDR_CMD 0x00
  20. #define CM3232_REG_ADDR_ALS 0x50
  21. #define CM3232_REG_ADDR_ID 0x53
  22. #define CM3232_CMD_ALS_DISABLE BIT(0)
  23. #define CM3232_CMD_ALS_IT_SHIFT 2
  24. #define CM3232_CMD_ALS_IT_MASK (BIT(2) | BIT(3) | BIT(4))
  25. #define CM3232_CMD_ALS_IT_DEFAULT (0x01 << CM3232_CMD_ALS_IT_SHIFT)
  26. #define CM3232_CMD_ALS_RESET BIT(6)
  27. #define CM3232_CMD_DEFAULT CM3232_CMD_ALS_IT_DEFAULT
  28. #define CM3232_HW_ID 0x32
  29. #define CM3232_CALIBSCALE_DEFAULT 100000
  30. #define CM3232_CALIBSCALE_RESOLUTION 100000
  31. #define CM3232_MLUX_PER_LUX 1000
  32. #define CM3232_MLUX_PER_BIT_DEFAULT 64
  33. #define CM3232_MLUX_PER_BIT_BASE_IT 100000
  34. static const struct {
  35. int val;
  36. int val2;
  37. u8 it;
  38. } cm3232_als_it_scales[] = {
  39. {0, 100000, 0}, /* 0.100000 */
  40. {0, 200000, 1}, /* 0.200000 */
  41. {0, 400000, 2}, /* 0.400000 */
  42. {0, 800000, 3}, /* 0.800000 */
  43. {1, 600000, 4}, /* 1.600000 */
  44. {3, 200000, 5}, /* 3.200000 */
  45. };
  46. struct cm3232_als_info {
  47. u8 regs_cmd_default;
  48. u8 hw_id;
  49. int calibscale;
  50. int mlux_per_bit;
  51. int mlux_per_bit_base_it;
  52. };
  53. static struct cm3232_als_info cm3232_als_info_default = {
  54. .regs_cmd_default = CM3232_CMD_DEFAULT,
  55. .hw_id = CM3232_HW_ID,
  56. .calibscale = CM3232_CALIBSCALE_DEFAULT,
  57. .mlux_per_bit = CM3232_MLUX_PER_BIT_DEFAULT,
  58. .mlux_per_bit_base_it = CM3232_MLUX_PER_BIT_BASE_IT,
  59. };
  60. struct cm3232_chip {
  61. struct i2c_client *client;
  62. struct cm3232_als_info *als_info;
  63. u8 regs_cmd;
  64. u16 regs_als;
  65. };
  66. /**
  67. * cm3232_reg_init() - Initialize CM3232
  68. * @chip: pointer of struct cm3232_chip.
  69. *
  70. * Check and initialize CM3232 ambient light sensor.
  71. *
  72. * Return: 0 for success; otherwise for error code.
  73. */
  74. static int cm3232_reg_init(struct cm3232_chip *chip)
  75. {
  76. struct i2c_client *client = chip->client;
  77. s32 ret;
  78. chip->als_info = &cm3232_als_info_default;
  79. /* Identify device */
  80. ret = i2c_smbus_read_word_data(client, CM3232_REG_ADDR_ID);
  81. if (ret < 0) {
  82. dev_err(&chip->client->dev, "Error reading addr_id\n");
  83. return ret;
  84. }
  85. if ((ret & 0xFF) != chip->als_info->hw_id)
  86. return -ENODEV;
  87. /* Disable and reset device */
  88. chip->regs_cmd = CM3232_CMD_ALS_DISABLE | CM3232_CMD_ALS_RESET;
  89. ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
  90. chip->regs_cmd);
  91. if (ret < 0) {
  92. dev_err(&chip->client->dev, "Error writing reg_cmd\n");
  93. return ret;
  94. }
  95. /* Register default value */
  96. chip->regs_cmd = chip->als_info->regs_cmd_default;
  97. /* Configure register */
  98. ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
  99. chip->regs_cmd);
  100. if (ret < 0)
  101. dev_err(&chip->client->dev, "Error writing reg_cmd\n");
  102. return ret;
  103. }
  104. /**
  105. * cm3232_read_als_it() - Get sensor integration time
  106. * @chip: pointer of struct cm3232_chip
  107. * @val: pointer of int to load the integration (sec).
  108. * @val2: pointer of int to load the integration time (microsecond).
  109. *
  110. * Report the current integration time.
  111. *
  112. * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
  113. */
  114. static int cm3232_read_als_it(struct cm3232_chip *chip, int *val, int *val2)
  115. {
  116. u16 als_it;
  117. int i;
  118. als_it = chip->regs_cmd;
  119. als_it &= CM3232_CMD_ALS_IT_MASK;
  120. als_it >>= CM3232_CMD_ALS_IT_SHIFT;
  121. for (i = 0; i < ARRAY_SIZE(cm3232_als_it_scales); i++) {
  122. if (als_it == cm3232_als_it_scales[i].it) {
  123. *val = cm3232_als_it_scales[i].val;
  124. *val2 = cm3232_als_it_scales[i].val2;
  125. return IIO_VAL_INT_PLUS_MICRO;
  126. }
  127. }
  128. return -EINVAL;
  129. }
  130. /**
  131. * cm3232_write_als_it() - Write sensor integration time
  132. * @chip: pointer of struct cm3232_chip.
  133. * @val: integration time in second.
  134. * @val2: integration time in microsecond.
  135. *
  136. * Convert integration time to sensor value.
  137. *
  138. * Return: i2c_smbus_write_byte_data command return value.
  139. */
  140. static int cm3232_write_als_it(struct cm3232_chip *chip, int val, int val2)
  141. {
  142. struct i2c_client *client = chip->client;
  143. u16 als_it, cmd;
  144. int i;
  145. s32 ret;
  146. for (i = 0; i < ARRAY_SIZE(cm3232_als_it_scales); i++) {
  147. if (val == cm3232_als_it_scales[i].val &&
  148. val2 == cm3232_als_it_scales[i].val2) {
  149. als_it = cm3232_als_it_scales[i].it;
  150. als_it <<= CM3232_CMD_ALS_IT_SHIFT;
  151. cmd = chip->regs_cmd & ~CM3232_CMD_ALS_IT_MASK;
  152. cmd |= als_it;
  153. ret = i2c_smbus_write_byte_data(client,
  154. CM3232_REG_ADDR_CMD,
  155. cmd);
  156. if (ret < 0)
  157. return ret;
  158. chip->regs_cmd = cmd;
  159. return 0;
  160. }
  161. }
  162. return -EINVAL;
  163. }
  164. /**
  165. * cm3232_get_lux() - report current lux value
  166. * @chip: pointer of struct cm3232_chip.
  167. *
  168. * Convert sensor data to lux. It depends on integration
  169. * time and calibscale variable.
  170. *
  171. * Return: Zero or positive value is lux, otherwise error code.
  172. */
  173. static int cm3232_get_lux(struct cm3232_chip *chip)
  174. {
  175. struct i2c_client *client = chip->client;
  176. struct cm3232_als_info *als_info = chip->als_info;
  177. int ret;
  178. int val, val2;
  179. int als_it;
  180. u64 lux;
  181. /* Calculate mlux per bit based on als_it */
  182. ret = cm3232_read_als_it(chip, &val, &val2);
  183. if (ret < 0)
  184. return -EINVAL;
  185. als_it = val * 1000000 + val2;
  186. lux = (__force u64)als_info->mlux_per_bit;
  187. lux *= als_info->mlux_per_bit_base_it;
  188. lux = div_u64(lux, als_it);
  189. ret = i2c_smbus_read_word_data(client, CM3232_REG_ADDR_ALS);
  190. if (ret < 0) {
  191. dev_err(&client->dev, "Error reading reg_addr_als\n");
  192. return ret;
  193. }
  194. chip->regs_als = (u16)ret;
  195. lux *= chip->regs_als;
  196. lux *= als_info->calibscale;
  197. lux = div_u64(lux, CM3232_CALIBSCALE_RESOLUTION);
  198. lux = div_u64(lux, CM3232_MLUX_PER_LUX);
  199. if (lux > 0xFFFF)
  200. lux = 0xFFFF;
  201. return (int)lux;
  202. }
  203. static int cm3232_read_raw(struct iio_dev *indio_dev,
  204. struct iio_chan_spec const *chan,
  205. int *val, int *val2, long mask)
  206. {
  207. struct cm3232_chip *chip = iio_priv(indio_dev);
  208. struct cm3232_als_info *als_info = chip->als_info;
  209. int ret;
  210. switch (mask) {
  211. case IIO_CHAN_INFO_PROCESSED:
  212. ret = cm3232_get_lux(chip);
  213. if (ret < 0)
  214. return ret;
  215. *val = ret;
  216. return IIO_VAL_INT;
  217. case IIO_CHAN_INFO_CALIBSCALE:
  218. *val = als_info->calibscale;
  219. return IIO_VAL_INT;
  220. case IIO_CHAN_INFO_INT_TIME:
  221. return cm3232_read_als_it(chip, val, val2);
  222. }
  223. return -EINVAL;
  224. }
  225. static int cm3232_write_raw(struct iio_dev *indio_dev,
  226. struct iio_chan_spec const *chan,
  227. int val, int val2, long mask)
  228. {
  229. struct cm3232_chip *chip = iio_priv(indio_dev);
  230. struct cm3232_als_info *als_info = chip->als_info;
  231. switch (mask) {
  232. case IIO_CHAN_INFO_CALIBSCALE:
  233. als_info->calibscale = val;
  234. return 0;
  235. case IIO_CHAN_INFO_INT_TIME:
  236. return cm3232_write_als_it(chip, val, val2);
  237. }
  238. return -EINVAL;
  239. }
  240. /**
  241. * cm3232_get_it_available() - Get available ALS IT value
  242. * @dev: pointer of struct device.
  243. * @attr: pointer of struct device_attribute.
  244. * @buf: pointer of return string buffer.
  245. *
  246. * Display the available integration time in second.
  247. *
  248. * Return: string length.
  249. */
  250. static ssize_t cm3232_get_it_available(struct device *dev,
  251. struct device_attribute *attr, char *buf)
  252. {
  253. int i, len;
  254. for (i = 0, len = 0; i < ARRAY_SIZE(cm3232_als_it_scales); i++)
  255. len += scnprintf(buf + len, PAGE_SIZE - len, "%u.%06u ",
  256. cm3232_als_it_scales[i].val,
  257. cm3232_als_it_scales[i].val2);
  258. return len + scnprintf(buf + len, PAGE_SIZE - len, "\n");
  259. }
  260. static const struct iio_chan_spec cm3232_channels[] = {
  261. {
  262. .type = IIO_LIGHT,
  263. .info_mask_separate =
  264. BIT(IIO_CHAN_INFO_PROCESSED) |
  265. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  266. BIT(IIO_CHAN_INFO_INT_TIME),
  267. }
  268. };
  269. static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
  270. S_IRUGO, cm3232_get_it_available, NULL, 0);
  271. static struct attribute *cm3232_attributes[] = {
  272. &iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
  273. NULL,
  274. };
  275. static const struct attribute_group cm3232_attribute_group = {
  276. .attrs = cm3232_attributes
  277. };
  278. static const struct iio_info cm3232_info = {
  279. .read_raw = &cm3232_read_raw,
  280. .write_raw = &cm3232_write_raw,
  281. .attrs = &cm3232_attribute_group,
  282. };
  283. static int cm3232_probe(struct i2c_client *client,
  284. const struct i2c_device_id *id)
  285. {
  286. struct cm3232_chip *chip;
  287. struct iio_dev *indio_dev;
  288. int ret;
  289. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  290. if (!indio_dev)
  291. return -ENOMEM;
  292. chip = iio_priv(indio_dev);
  293. i2c_set_clientdata(client, indio_dev);
  294. chip->client = client;
  295. indio_dev->dev.parent = &client->dev;
  296. indio_dev->channels = cm3232_channels;
  297. indio_dev->num_channels = ARRAY_SIZE(cm3232_channels);
  298. indio_dev->info = &cm3232_info;
  299. indio_dev->name = id->name;
  300. indio_dev->modes = INDIO_DIRECT_MODE;
  301. ret = cm3232_reg_init(chip);
  302. if (ret) {
  303. dev_err(&client->dev,
  304. "%s: register init failed\n",
  305. __func__);
  306. return ret;
  307. }
  308. return iio_device_register(indio_dev);
  309. }
  310. static int cm3232_remove(struct i2c_client *client)
  311. {
  312. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  313. i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
  314. CM3232_CMD_ALS_DISABLE);
  315. iio_device_unregister(indio_dev);
  316. return 0;
  317. }
  318. static const struct i2c_device_id cm3232_id[] = {
  319. {"cm3232", 0},
  320. {}
  321. };
  322. #ifdef CONFIG_PM_SLEEP
  323. static int cm3232_suspend(struct device *dev)
  324. {
  325. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  326. struct cm3232_chip *chip = iio_priv(indio_dev);
  327. struct i2c_client *client = chip->client;
  328. int ret;
  329. chip->regs_cmd |= CM3232_CMD_ALS_DISABLE;
  330. ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
  331. chip->regs_cmd);
  332. return ret;
  333. }
  334. static int cm3232_resume(struct device *dev)
  335. {
  336. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  337. struct cm3232_chip *chip = iio_priv(indio_dev);
  338. struct i2c_client *client = chip->client;
  339. int ret;
  340. chip->regs_cmd &= ~CM3232_CMD_ALS_DISABLE;
  341. ret = i2c_smbus_write_byte_data(client, CM3232_REG_ADDR_CMD,
  342. chip->regs_cmd | CM3232_CMD_ALS_RESET);
  343. return ret;
  344. }
  345. static const struct dev_pm_ops cm3232_pm_ops = {
  346. SET_SYSTEM_SLEEP_PM_OPS(cm3232_suspend, cm3232_resume)};
  347. #endif
  348. MODULE_DEVICE_TABLE(i2c, cm3232_id);
  349. static const struct of_device_id cm3232_of_match[] = {
  350. {.compatible = "capella,cm3232"},
  351. {}
  352. };
  353. MODULE_DEVICE_TABLE(of, cm3232_of_match);
  354. static struct i2c_driver cm3232_driver = {
  355. .driver = {
  356. .name = "cm3232",
  357. .of_match_table = of_match_ptr(cm3232_of_match),
  358. #ifdef CONFIG_PM_SLEEP
  359. .pm = &cm3232_pm_ops,
  360. #endif
  361. },
  362. .id_table = cm3232_id,
  363. .probe = cm3232_probe,
  364. .remove = cm3232_remove,
  365. };
  366. module_i2c_driver(cm3232_driver);
  367. MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
  368. MODULE_DESCRIPTION("CM3232 ambient light sensor driver");
  369. MODULE_LICENSE("GPL");