cm32181.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Capella Microsystems Inc.
  4. * Author: Kevin Tsai <[email protected]>
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/i2c.h>
  10. #include <linux/mutex.h>
  11. #include <linux/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/sysfs.h>
  17. #include <linux/iio/events.h>
  18. #include <linux/init.h>
  19. /* Registers Address */
  20. #define CM32181_REG_ADDR_CMD 0x00
  21. #define CM32181_REG_ADDR_WH 0x01
  22. #define CM32181_REG_ADDR_WL 0x02
  23. #define CM32181_REG_ADDR_TEST 0x03
  24. #define CM32181_REG_ADDR_ALS 0x04
  25. #define CM32181_REG_ADDR_STATUS 0x06
  26. #define CM32181_REG_ADDR_ID 0x07
  27. /* Number of Configurable Registers */
  28. #define CM32181_CONF_REG_NUM 4
  29. /* CMD register */
  30. #define CM32181_CMD_ALS_DISABLE BIT(0)
  31. #define CM32181_CMD_ALS_INT_EN BIT(1)
  32. #define CM32181_CMD_ALS_THRES_WINDOW BIT(2)
  33. #define CM32181_CMD_ALS_PERS_SHIFT 4
  34. #define CM32181_CMD_ALS_PERS_MASK (0x03 << CM32181_CMD_ALS_PERS_SHIFT)
  35. #define CM32181_CMD_ALS_PERS_DEFAULT (0x01 << CM32181_CMD_ALS_PERS_SHIFT)
  36. #define CM32181_CMD_ALS_IT_SHIFT 6
  37. #define CM32181_CMD_ALS_IT_MASK (0x0F << CM32181_CMD_ALS_IT_SHIFT)
  38. #define CM32181_CMD_ALS_IT_DEFAULT (0x00 << CM32181_CMD_ALS_IT_SHIFT)
  39. #define CM32181_CMD_ALS_SM_SHIFT 11
  40. #define CM32181_CMD_ALS_SM_MASK (0x03 << CM32181_CMD_ALS_SM_SHIFT)
  41. #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT)
  42. #define CM32181_LUX_PER_BIT 500 /* ALS_SM=01 IT=800ms */
  43. #define CM32181_LUX_PER_BIT_RESOLUTION 100000
  44. #define CM32181_LUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */
  45. #define CM32181_CALIBSCALE_DEFAULT 100000
  46. #define CM32181_CALIBSCALE_RESOLUTION 100000
  47. #define SMBUS_ALERT_RESPONSE_ADDRESS 0x0c
  48. /* CPM0 Index 0: device-id (3218 or 32181), 1: Unknown, 2: init_regs_bitmap */
  49. #define CPM0_REGS_BITMAP 2
  50. #define CPM0_HEADER_SIZE 3
  51. /* CPM1 Index 0: lux_per_bit, 1: calibscale, 2: resolution (100000) */
  52. #define CPM1_LUX_PER_BIT 0
  53. #define CPM1_CALIBSCALE 1
  54. #define CPM1_SIZE 3
  55. /* CM3218 Family */
  56. static const int cm3218_als_it_bits[] = { 0, 1, 2, 3 };
  57. static const int cm3218_als_it_values[] = { 100000, 200000, 400000, 800000 };
  58. /* CM32181 Family */
  59. static const int cm32181_als_it_bits[] = { 12, 8, 0, 1, 2, 3 };
  60. static const int cm32181_als_it_values[] = {
  61. 25000, 50000, 100000, 200000, 400000, 800000
  62. };
  63. struct cm32181_chip {
  64. struct i2c_client *client;
  65. struct device *dev;
  66. struct mutex lock;
  67. u16 conf_regs[CM32181_CONF_REG_NUM];
  68. unsigned long init_regs_bitmap;
  69. int calibscale;
  70. int lux_per_bit;
  71. int lux_per_bit_base_it;
  72. int num_als_it;
  73. const int *als_it_bits;
  74. const int *als_it_values;
  75. };
  76. static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2);
  77. #ifdef CONFIG_ACPI
  78. /**
  79. * cm32181_acpi_get_cpm() - Get CPM object from ACPI
  80. * @dev: pointer of struct device.
  81. * @obj_name: pointer of ACPI object name.
  82. * @values: pointer of array for return elements.
  83. * @count: maximum size of return array.
  84. *
  85. * Convert ACPI CPM table to array.
  86. *
  87. * Return: -ENODEV for fail. Otherwise is number of elements.
  88. */
  89. static int cm32181_acpi_get_cpm(struct device *dev, char *obj_name,
  90. u64 *values, int count)
  91. {
  92. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  93. union acpi_object *cpm, *elem;
  94. acpi_handle handle;
  95. acpi_status status;
  96. int i;
  97. handle = ACPI_HANDLE(dev);
  98. if (!handle)
  99. return -ENODEV;
  100. status = acpi_evaluate_object(handle, obj_name, NULL, &buffer);
  101. if (ACPI_FAILURE(status)) {
  102. dev_err(dev, "object %s not found\n", obj_name);
  103. return -ENODEV;
  104. }
  105. cpm = buffer.pointer;
  106. if (cpm->package.count > count)
  107. dev_warn(dev, "%s table contains %u values, only using first %d values\n",
  108. obj_name, cpm->package.count, count);
  109. count = min_t(int, cpm->package.count, count);
  110. for (i = 0; i < count; i++) {
  111. elem = &(cpm->package.elements[i]);
  112. values[i] = elem->integer.value;
  113. }
  114. kfree(buffer.pointer);
  115. return count;
  116. }
  117. static void cm32181_acpi_parse_cpm_tables(struct cm32181_chip *cm32181)
  118. {
  119. u64 vals[CPM0_HEADER_SIZE + CM32181_CONF_REG_NUM];
  120. struct device *dev = cm32181->dev;
  121. int i, count;
  122. count = cm32181_acpi_get_cpm(dev, "CPM0", vals, ARRAY_SIZE(vals));
  123. if (count <= CPM0_HEADER_SIZE)
  124. return;
  125. count -= CPM0_HEADER_SIZE;
  126. cm32181->init_regs_bitmap = vals[CPM0_REGS_BITMAP];
  127. cm32181->init_regs_bitmap &= GENMASK(count - 1, 0);
  128. for_each_set_bit(i, &cm32181->init_regs_bitmap, count)
  129. cm32181->conf_regs[i] = vals[CPM0_HEADER_SIZE + i];
  130. count = cm32181_acpi_get_cpm(dev, "CPM1", vals, ARRAY_SIZE(vals));
  131. if (count != CPM1_SIZE)
  132. return;
  133. cm32181->lux_per_bit = vals[CPM1_LUX_PER_BIT];
  134. /* Check for uncalibrated devices */
  135. if (vals[CPM1_CALIBSCALE] == CM32181_CALIBSCALE_DEFAULT)
  136. return;
  137. cm32181->calibscale = vals[CPM1_CALIBSCALE];
  138. /* CPM1 lux_per_bit is for the current it value */
  139. cm32181_read_als_it(cm32181, &cm32181->lux_per_bit_base_it);
  140. }
  141. #else
  142. static void cm32181_acpi_parse_cpm_tables(struct cm32181_chip *cm32181)
  143. {
  144. }
  145. #endif /* CONFIG_ACPI */
  146. /**
  147. * cm32181_reg_init() - Initialize CM32181 registers
  148. * @cm32181: pointer of struct cm32181.
  149. *
  150. * Initialize CM32181 ambient light sensor register to default values.
  151. *
  152. * Return: 0 for success; otherwise for error code.
  153. */
  154. static int cm32181_reg_init(struct cm32181_chip *cm32181)
  155. {
  156. struct i2c_client *client = cm32181->client;
  157. int i;
  158. s32 ret;
  159. ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID);
  160. if (ret < 0)
  161. return ret;
  162. /* check device ID */
  163. switch (ret & 0xFF) {
  164. case 0x18: /* CM3218 */
  165. cm32181->num_als_it = ARRAY_SIZE(cm3218_als_it_bits);
  166. cm32181->als_it_bits = cm3218_als_it_bits;
  167. cm32181->als_it_values = cm3218_als_it_values;
  168. break;
  169. case 0x81: /* CM32181 */
  170. case 0x82: /* CM32182, fully compat. with CM32181 */
  171. cm32181->num_als_it = ARRAY_SIZE(cm32181_als_it_bits);
  172. cm32181->als_it_bits = cm32181_als_it_bits;
  173. cm32181->als_it_values = cm32181_als_it_values;
  174. break;
  175. default:
  176. return -ENODEV;
  177. }
  178. /* Default Values */
  179. cm32181->conf_regs[CM32181_REG_ADDR_CMD] =
  180. CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT;
  181. cm32181->init_regs_bitmap = BIT(CM32181_REG_ADDR_CMD);
  182. cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT;
  183. cm32181->lux_per_bit = CM32181_LUX_PER_BIT;
  184. cm32181->lux_per_bit_base_it = CM32181_LUX_PER_BIT_BASE_IT;
  185. if (ACPI_HANDLE(cm32181->dev))
  186. cm32181_acpi_parse_cpm_tables(cm32181);
  187. /* Initialize registers*/
  188. for_each_set_bit(i, &cm32181->init_regs_bitmap, CM32181_CONF_REG_NUM) {
  189. ret = i2c_smbus_write_word_data(client, i,
  190. cm32181->conf_regs[i]);
  191. if (ret < 0)
  192. return ret;
  193. }
  194. return 0;
  195. }
  196. /**
  197. * cm32181_read_als_it() - Get sensor integration time (ms)
  198. * @cm32181: pointer of struct cm32181
  199. * @val2: pointer of int to load the als_it value.
  200. *
  201. * Report the current integration time in milliseconds.
  202. *
  203. * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
  204. */
  205. static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2)
  206. {
  207. u16 als_it;
  208. int i;
  209. als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD];
  210. als_it &= CM32181_CMD_ALS_IT_MASK;
  211. als_it >>= CM32181_CMD_ALS_IT_SHIFT;
  212. for (i = 0; i < cm32181->num_als_it; i++) {
  213. if (als_it == cm32181->als_it_bits[i]) {
  214. *val2 = cm32181->als_it_values[i];
  215. return IIO_VAL_INT_PLUS_MICRO;
  216. }
  217. }
  218. return -EINVAL;
  219. }
  220. /**
  221. * cm32181_write_als_it() - Write sensor integration time
  222. * @cm32181: pointer of struct cm32181.
  223. * @val: integration time by millisecond.
  224. *
  225. * Convert integration time (ms) to sensor value.
  226. *
  227. * Return: i2c_smbus_write_word_data command return value.
  228. */
  229. static int cm32181_write_als_it(struct cm32181_chip *cm32181, int val)
  230. {
  231. struct i2c_client *client = cm32181->client;
  232. u16 als_it;
  233. int ret, i, n;
  234. n = cm32181->num_als_it;
  235. for (i = 0; i < n; i++)
  236. if (val <= cm32181->als_it_values[i])
  237. break;
  238. if (i >= n)
  239. i = n - 1;
  240. als_it = cm32181->als_it_bits[i];
  241. als_it <<= CM32181_CMD_ALS_IT_SHIFT;
  242. mutex_lock(&cm32181->lock);
  243. cm32181->conf_regs[CM32181_REG_ADDR_CMD] &=
  244. ~CM32181_CMD_ALS_IT_MASK;
  245. cm32181->conf_regs[CM32181_REG_ADDR_CMD] |=
  246. als_it;
  247. ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
  248. cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
  249. mutex_unlock(&cm32181->lock);
  250. return ret;
  251. }
  252. /**
  253. * cm32181_get_lux() - report current lux value
  254. * @cm32181: pointer of struct cm32181.
  255. *
  256. * Convert sensor raw data to lux. It depends on integration
  257. * time and calibscale variable.
  258. *
  259. * Return: Positive value is lux, otherwise is error code.
  260. */
  261. static int cm32181_get_lux(struct cm32181_chip *cm32181)
  262. {
  263. struct i2c_client *client = cm32181->client;
  264. int ret;
  265. int als_it;
  266. u64 lux;
  267. ret = cm32181_read_als_it(cm32181, &als_it);
  268. if (ret < 0)
  269. return -EINVAL;
  270. lux = cm32181->lux_per_bit;
  271. lux *= cm32181->lux_per_bit_base_it;
  272. lux = div_u64(lux, als_it);
  273. ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
  274. if (ret < 0)
  275. return ret;
  276. lux *= ret;
  277. lux *= cm32181->calibscale;
  278. lux = div_u64(lux, CM32181_CALIBSCALE_RESOLUTION);
  279. lux = div_u64(lux, CM32181_LUX_PER_BIT_RESOLUTION);
  280. if (lux > 0xFFFF)
  281. lux = 0xFFFF;
  282. return lux;
  283. }
  284. static int cm32181_read_raw(struct iio_dev *indio_dev,
  285. struct iio_chan_spec const *chan,
  286. int *val, int *val2, long mask)
  287. {
  288. struct cm32181_chip *cm32181 = iio_priv(indio_dev);
  289. int ret;
  290. switch (mask) {
  291. case IIO_CHAN_INFO_PROCESSED:
  292. ret = cm32181_get_lux(cm32181);
  293. if (ret < 0)
  294. return ret;
  295. *val = ret;
  296. return IIO_VAL_INT;
  297. case IIO_CHAN_INFO_CALIBSCALE:
  298. *val = cm32181->calibscale;
  299. return IIO_VAL_INT;
  300. case IIO_CHAN_INFO_INT_TIME:
  301. *val = 0;
  302. ret = cm32181_read_als_it(cm32181, val2);
  303. return ret;
  304. }
  305. return -EINVAL;
  306. }
  307. static int cm32181_write_raw(struct iio_dev *indio_dev,
  308. struct iio_chan_spec const *chan,
  309. int val, int val2, long mask)
  310. {
  311. struct cm32181_chip *cm32181 = iio_priv(indio_dev);
  312. int ret;
  313. switch (mask) {
  314. case IIO_CHAN_INFO_CALIBSCALE:
  315. cm32181->calibscale = val;
  316. return val;
  317. case IIO_CHAN_INFO_INT_TIME:
  318. ret = cm32181_write_als_it(cm32181, val2);
  319. return ret;
  320. }
  321. return -EINVAL;
  322. }
  323. /**
  324. * cm32181_get_it_available() - Get available ALS IT value
  325. * @dev: pointer of struct device.
  326. * @attr: pointer of struct device_attribute.
  327. * @buf: pointer of return string buffer.
  328. *
  329. * Display the available integration time values by millisecond.
  330. *
  331. * Return: string length.
  332. */
  333. static ssize_t cm32181_get_it_available(struct device *dev,
  334. struct device_attribute *attr, char *buf)
  335. {
  336. struct cm32181_chip *cm32181 = iio_priv(dev_to_iio_dev(dev));
  337. int i, n, len;
  338. n = cm32181->num_als_it;
  339. for (i = 0, len = 0; i < n; i++)
  340. len += sprintf(buf + len, "0.%06u ", cm32181->als_it_values[i]);
  341. return len + sprintf(buf + len, "\n");
  342. }
  343. static const struct iio_chan_spec cm32181_channels[] = {
  344. {
  345. .type = IIO_LIGHT,
  346. .info_mask_separate =
  347. BIT(IIO_CHAN_INFO_PROCESSED) |
  348. BIT(IIO_CHAN_INFO_CALIBSCALE) |
  349. BIT(IIO_CHAN_INFO_INT_TIME),
  350. }
  351. };
  352. static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
  353. S_IRUGO, cm32181_get_it_available, NULL, 0);
  354. static struct attribute *cm32181_attributes[] = {
  355. &iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
  356. NULL,
  357. };
  358. static const struct attribute_group cm32181_attribute_group = {
  359. .attrs = cm32181_attributes
  360. };
  361. static const struct iio_info cm32181_info = {
  362. .read_raw = &cm32181_read_raw,
  363. .write_raw = &cm32181_write_raw,
  364. .attrs = &cm32181_attribute_group,
  365. };
  366. static void cm32181_unregister_dummy_client(void *data)
  367. {
  368. struct i2c_client *client = data;
  369. /* Unregister the dummy client */
  370. i2c_unregister_device(client);
  371. }
  372. static int cm32181_probe(struct i2c_client *client)
  373. {
  374. struct device *dev = &client->dev;
  375. struct cm32181_chip *cm32181;
  376. struct iio_dev *indio_dev;
  377. int ret;
  378. indio_dev = devm_iio_device_alloc(dev, sizeof(*cm32181));
  379. if (!indio_dev)
  380. return -ENOMEM;
  381. i2c_set_clientdata(client, indio_dev);
  382. /*
  383. * Some ACPI systems list 2 I2C resources for the CM3218 sensor, the
  384. * SMBus Alert Response Address (ARA, 0x0c) and the actual I2C address.
  385. * Detect this and take the following step to deal with it:
  386. * 1. When a SMBus Alert capable sensor has an Alert asserted, it will
  387. * not respond on its actual I2C address. Read a byte from the ARA
  388. * to clear any pending Alerts.
  389. * 2. Create a "dummy" client for the actual I2C address and
  390. * use that client to communicate with the sensor.
  391. */
  392. if (ACPI_HANDLE(dev) && client->addr == SMBUS_ALERT_RESPONSE_ADDRESS) {
  393. struct i2c_board_info board_info = { .type = "dummy" };
  394. i2c_smbus_read_byte(client);
  395. client = i2c_acpi_new_device(dev, 1, &board_info);
  396. if (IS_ERR(client))
  397. return PTR_ERR(client);
  398. ret = devm_add_action_or_reset(dev, cm32181_unregister_dummy_client, client);
  399. if (ret)
  400. return ret;
  401. }
  402. cm32181 = iio_priv(indio_dev);
  403. cm32181->client = client;
  404. cm32181->dev = dev;
  405. mutex_init(&cm32181->lock);
  406. indio_dev->channels = cm32181_channels;
  407. indio_dev->num_channels = ARRAY_SIZE(cm32181_channels);
  408. indio_dev->info = &cm32181_info;
  409. indio_dev->name = dev_name(dev);
  410. indio_dev->modes = INDIO_DIRECT_MODE;
  411. ret = cm32181_reg_init(cm32181);
  412. if (ret) {
  413. dev_err(dev, "%s: register init failed\n", __func__);
  414. return ret;
  415. }
  416. ret = devm_iio_device_register(dev, indio_dev);
  417. if (ret) {
  418. dev_err(dev, "%s: regist device failed\n", __func__);
  419. return ret;
  420. }
  421. return 0;
  422. }
  423. static int cm32181_suspend(struct device *dev)
  424. {
  425. struct cm32181_chip *cm32181 = iio_priv(dev_get_drvdata(dev));
  426. struct i2c_client *client = cm32181->client;
  427. return i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
  428. CM32181_CMD_ALS_DISABLE);
  429. }
  430. static int cm32181_resume(struct device *dev)
  431. {
  432. struct cm32181_chip *cm32181 = iio_priv(dev_get_drvdata(dev));
  433. struct i2c_client *client = cm32181->client;
  434. return i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
  435. cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
  436. }
  437. static DEFINE_SIMPLE_DEV_PM_OPS(cm32181_pm_ops, cm32181_suspend, cm32181_resume);
  438. static const struct of_device_id cm32181_of_match[] = {
  439. { .compatible = "capella,cm3218" },
  440. { .compatible = "capella,cm32181" },
  441. { }
  442. };
  443. MODULE_DEVICE_TABLE(of, cm32181_of_match);
  444. #ifdef CONFIG_ACPI
  445. static const struct acpi_device_id cm32181_acpi_match[] = {
  446. { "CPLM3218", 0 },
  447. { }
  448. };
  449. MODULE_DEVICE_TABLE(acpi, cm32181_acpi_match);
  450. #endif
  451. static struct i2c_driver cm32181_driver = {
  452. .driver = {
  453. .name = "cm32181",
  454. .acpi_match_table = ACPI_PTR(cm32181_acpi_match),
  455. .of_match_table = cm32181_of_match,
  456. .pm = pm_sleep_ptr(&cm32181_pm_ops),
  457. },
  458. .probe_new = cm32181_probe,
  459. };
  460. module_i2c_driver(cm32181_driver);
  461. MODULE_AUTHOR("Kevin Tsai <[email protected]>");
  462. MODULE_DESCRIPTION("CM32181 ambient light sensor driver");
  463. MODULE_LICENSE("GPL");