jsa1212.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * JSA1212 Ambient Light & Proximity Sensor Driver
  3. *
  4. * Copyright (c) 2014, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * JSA1212 I2C slave address: 0x44(ADDR tied to GND), 0x45(ADDR tied to VDD)
  16. *
  17. * TODO: Interrupt support, thresholds, range support.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/i2c.h>
  24. #include <linux/mutex.h>
  25. #include <linux/acpi.h>
  26. #include <linux/regmap.h>
  27. #include <linux/iio/iio.h>
  28. #include <linux/iio/sysfs.h>
  29. /* JSA1212 reg address */
  30. #define JSA1212_CONF_REG 0x01
  31. #define JSA1212_INT_REG 0x02
  32. #define JSA1212_PXS_LT_REG 0x03
  33. #define JSA1212_PXS_HT_REG 0x04
  34. #define JSA1212_ALS_TH1_REG 0x05
  35. #define JSA1212_ALS_TH2_REG 0x06
  36. #define JSA1212_ALS_TH3_REG 0x07
  37. #define JSA1212_PXS_DATA_REG 0x08
  38. #define JSA1212_ALS_DT1_REG 0x09
  39. #define JSA1212_ALS_DT2_REG 0x0A
  40. #define JSA1212_ALS_RNG_REG 0x0B
  41. #define JSA1212_MAX_REG 0x0C
  42. /* JSA1212 reg masks */
  43. #define JSA1212_CONF_MASK 0xFF
  44. #define JSA1212_INT_MASK 0xFF
  45. #define JSA1212_PXS_LT_MASK 0xFF
  46. #define JSA1212_PXS_HT_MASK 0xFF
  47. #define JSA1212_ALS_TH1_MASK 0xFF
  48. #define JSA1212_ALS_TH2_LT_MASK 0x0F
  49. #define JSA1212_ALS_TH2_HT_MASK 0xF0
  50. #define JSA1212_ALS_TH3_MASK 0xFF
  51. #define JSA1212_PXS_DATA_MASK 0xFF
  52. #define JSA1212_ALS_DATA_MASK 0x0FFF
  53. #define JSA1212_ALS_DT1_MASK 0xFF
  54. #define JSA1212_ALS_DT2_MASK 0x0F
  55. #define JSA1212_ALS_RNG_MASK 0x07
  56. /* JSA1212 CONF REG bits */
  57. #define JSA1212_CONF_PXS_MASK 0x80
  58. #define JSA1212_CONF_PXS_ENABLE 0x80
  59. #define JSA1212_CONF_PXS_DISABLE 0x00
  60. #define JSA1212_CONF_ALS_MASK 0x04
  61. #define JSA1212_CONF_ALS_ENABLE 0x04
  62. #define JSA1212_CONF_ALS_DISABLE 0x00
  63. #define JSA1212_CONF_IRDR_MASK 0x08
  64. /* Proxmity sensing IRDR current sink settings */
  65. #define JSA1212_CONF_IRDR_200MA 0x08
  66. #define JSA1212_CONF_IRDR_100MA 0x00
  67. #define JSA1212_CONF_PXS_SLP_MASK 0x70
  68. #define JSA1212_CONF_PXS_SLP_0MS 0x70
  69. #define JSA1212_CONF_PXS_SLP_12MS 0x60
  70. #define JSA1212_CONF_PXS_SLP_50MS 0x50
  71. #define JSA1212_CONF_PXS_SLP_75MS 0x40
  72. #define JSA1212_CONF_PXS_SLP_100MS 0x30
  73. #define JSA1212_CONF_PXS_SLP_200MS 0x20
  74. #define JSA1212_CONF_PXS_SLP_400MS 0x10
  75. #define JSA1212_CONF_PXS_SLP_800MS 0x00
  76. /* JSA1212 INT REG bits */
  77. #define JSA1212_INT_CTRL_MASK 0x01
  78. #define JSA1212_INT_CTRL_EITHER 0x00
  79. #define JSA1212_INT_CTRL_BOTH 0x01
  80. #define JSA1212_INT_ALS_PRST_MASK 0x06
  81. #define JSA1212_INT_ALS_PRST_1CONV 0x00
  82. #define JSA1212_INT_ALS_PRST_4CONV 0x02
  83. #define JSA1212_INT_ALS_PRST_8CONV 0x04
  84. #define JSA1212_INT_ALS_PRST_16CONV 0x06
  85. #define JSA1212_INT_ALS_FLAG_MASK 0x08
  86. #define JSA1212_INT_ALS_FLAG_CLR 0x00
  87. #define JSA1212_INT_PXS_PRST_MASK 0x60
  88. #define JSA1212_INT_PXS_PRST_1CONV 0x00
  89. #define JSA1212_INT_PXS_PRST_4CONV 0x20
  90. #define JSA1212_INT_PXS_PRST_8CONV 0x40
  91. #define JSA1212_INT_PXS_PRST_16CONV 0x60
  92. #define JSA1212_INT_PXS_FLAG_MASK 0x80
  93. #define JSA1212_INT_PXS_FLAG_CLR 0x00
  94. /* JSA1212 ALS RNG REG bits */
  95. #define JSA1212_ALS_RNG_0_2048 0x00
  96. #define JSA1212_ALS_RNG_0_1024 0x01
  97. #define JSA1212_ALS_RNG_0_512 0x02
  98. #define JSA1212_ALS_RNG_0_256 0x03
  99. #define JSA1212_ALS_RNG_0_128 0x04
  100. /* JSA1212 INT threshold range */
  101. #define JSA1212_ALS_TH_MIN 0x0000
  102. #define JSA1212_ALS_TH_MAX 0x0FFF
  103. #define JSA1212_PXS_TH_MIN 0x00
  104. #define JSA1212_PXS_TH_MAX 0xFF
  105. #define JSA1212_ALS_DELAY_MS 200
  106. #define JSA1212_PXS_DELAY_MS 100
  107. #define JSA1212_DRIVER_NAME "jsa1212"
  108. #define JSA1212_REGMAP_NAME "jsa1212_regmap"
  109. enum jsa1212_op_mode {
  110. JSA1212_OPMODE_ALS_EN,
  111. JSA1212_OPMODE_PXS_EN,
  112. };
  113. struct jsa1212_data {
  114. struct i2c_client *client;
  115. struct mutex lock;
  116. u8 als_rng_idx;
  117. bool als_en; /* ALS enable status */
  118. bool pxs_en; /* proximity enable status */
  119. struct regmap *regmap;
  120. };
  121. /* ALS range idx to val mapping */
  122. static const int jsa1212_als_range_val[] = {2048, 1024, 512, 256, 128,
  123. 128, 128, 128};
  124. /* Enables or disables ALS function based on status */
  125. static int jsa1212_als_enable(struct jsa1212_data *data, u8 status)
  126. {
  127. int ret;
  128. ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
  129. JSA1212_CONF_ALS_MASK,
  130. status);
  131. if (ret < 0)
  132. return ret;
  133. data->als_en = !!status;
  134. return 0;
  135. }
  136. /* Enables or disables PXS function based on status */
  137. static int jsa1212_pxs_enable(struct jsa1212_data *data, u8 status)
  138. {
  139. int ret;
  140. ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
  141. JSA1212_CONF_PXS_MASK,
  142. status);
  143. if (ret < 0)
  144. return ret;
  145. data->pxs_en = !!status;
  146. return 0;
  147. }
  148. static int jsa1212_read_als_data(struct jsa1212_data *data,
  149. unsigned int *val)
  150. {
  151. int ret;
  152. __le16 als_data;
  153. ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
  154. if (ret < 0)
  155. return ret;
  156. /* Delay for data output */
  157. msleep(JSA1212_ALS_DELAY_MS);
  158. /* Read 12 bit data */
  159. ret = regmap_bulk_read(data->regmap, JSA1212_ALS_DT1_REG, &als_data, 2);
  160. if (ret < 0) {
  161. dev_err(&data->client->dev, "als data read err\n");
  162. goto als_data_read_err;
  163. }
  164. *val = le16_to_cpu(als_data);
  165. als_data_read_err:
  166. return jsa1212_als_enable(data, JSA1212_CONF_ALS_DISABLE);
  167. }
  168. static int jsa1212_read_pxs_data(struct jsa1212_data *data,
  169. unsigned int *val)
  170. {
  171. int ret;
  172. unsigned int pxs_data;
  173. ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
  174. if (ret < 0)
  175. return ret;
  176. /* Delay for data output */
  177. msleep(JSA1212_PXS_DELAY_MS);
  178. /* Read out all data */
  179. ret = regmap_read(data->regmap, JSA1212_PXS_DATA_REG, &pxs_data);
  180. if (ret < 0) {
  181. dev_err(&data->client->dev, "pxs data read err\n");
  182. goto pxs_data_read_err;
  183. }
  184. *val = pxs_data & JSA1212_PXS_DATA_MASK;
  185. pxs_data_read_err:
  186. return jsa1212_pxs_enable(data, JSA1212_CONF_PXS_DISABLE);
  187. }
  188. static int jsa1212_read_raw(struct iio_dev *indio_dev,
  189. struct iio_chan_spec const *chan,
  190. int *val, int *val2, long mask)
  191. {
  192. int ret;
  193. struct jsa1212_data *data = iio_priv(indio_dev);
  194. switch (mask) {
  195. case IIO_CHAN_INFO_RAW:
  196. mutex_lock(&data->lock);
  197. switch (chan->type) {
  198. case IIO_LIGHT:
  199. ret = jsa1212_read_als_data(data, val);
  200. break;
  201. case IIO_PROXIMITY:
  202. ret = jsa1212_read_pxs_data(data, val);
  203. break;
  204. default:
  205. ret = -EINVAL;
  206. break;
  207. }
  208. mutex_unlock(&data->lock);
  209. return ret < 0 ? ret : IIO_VAL_INT;
  210. case IIO_CHAN_INFO_SCALE:
  211. switch (chan->type) {
  212. case IIO_LIGHT:
  213. *val = jsa1212_als_range_val[data->als_rng_idx];
  214. *val2 = BIT(12); /* Max 12 bit value */
  215. return IIO_VAL_FRACTIONAL;
  216. default:
  217. break;
  218. }
  219. break;
  220. default:
  221. break;
  222. }
  223. return -EINVAL;
  224. }
  225. static const struct iio_chan_spec jsa1212_channels[] = {
  226. {
  227. .type = IIO_LIGHT,
  228. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  229. BIT(IIO_CHAN_INFO_SCALE),
  230. },
  231. {
  232. .type = IIO_PROXIMITY,
  233. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  234. }
  235. };
  236. static const struct iio_info jsa1212_info = {
  237. .driver_module = THIS_MODULE,
  238. .read_raw = &jsa1212_read_raw,
  239. };
  240. static int jsa1212_chip_init(struct jsa1212_data *data)
  241. {
  242. int ret;
  243. ret = regmap_write(data->regmap, JSA1212_CONF_REG,
  244. (JSA1212_CONF_PXS_SLP_50MS |
  245. JSA1212_CONF_IRDR_200MA));
  246. if (ret < 0)
  247. return ret;
  248. ret = regmap_write(data->regmap, JSA1212_INT_REG,
  249. JSA1212_INT_ALS_PRST_4CONV);
  250. if (ret < 0)
  251. return ret;
  252. data->als_rng_idx = JSA1212_ALS_RNG_0_2048;
  253. return 0;
  254. }
  255. static bool jsa1212_is_volatile_reg(struct device *dev, unsigned int reg)
  256. {
  257. switch (reg) {
  258. case JSA1212_PXS_DATA_REG:
  259. case JSA1212_ALS_DT1_REG:
  260. case JSA1212_ALS_DT2_REG:
  261. case JSA1212_INT_REG:
  262. return true;
  263. default:
  264. return false;
  265. }
  266. }
  267. static const struct regmap_config jsa1212_regmap_config = {
  268. .name = JSA1212_REGMAP_NAME,
  269. .reg_bits = 8,
  270. .val_bits = 8,
  271. .max_register = JSA1212_MAX_REG,
  272. .cache_type = REGCACHE_RBTREE,
  273. .volatile_reg = jsa1212_is_volatile_reg,
  274. };
  275. static int jsa1212_probe(struct i2c_client *client,
  276. const struct i2c_device_id *id)
  277. {
  278. struct jsa1212_data *data;
  279. struct iio_dev *indio_dev;
  280. struct regmap *regmap;
  281. int ret;
  282. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  283. if (!indio_dev)
  284. return -ENOMEM;
  285. regmap = devm_regmap_init_i2c(client, &jsa1212_regmap_config);
  286. if (IS_ERR(regmap)) {
  287. dev_err(&client->dev, "Regmap initialization failed.\n");
  288. return PTR_ERR(regmap);
  289. }
  290. data = iio_priv(indio_dev);
  291. i2c_set_clientdata(client, indio_dev);
  292. data->client = client;
  293. data->regmap = regmap;
  294. mutex_init(&data->lock);
  295. ret = jsa1212_chip_init(data);
  296. if (ret < 0)
  297. return ret;
  298. indio_dev->dev.parent = &client->dev;
  299. indio_dev->channels = jsa1212_channels;
  300. indio_dev->num_channels = ARRAY_SIZE(jsa1212_channels);
  301. indio_dev->name = JSA1212_DRIVER_NAME;
  302. indio_dev->modes = INDIO_DIRECT_MODE;
  303. indio_dev->info = &jsa1212_info;
  304. ret = iio_device_register(indio_dev);
  305. if (ret < 0)
  306. dev_err(&client->dev, "%s: register device failed\n", __func__);
  307. return ret;
  308. }
  309. /* power off the device */
  310. static int jsa1212_power_off(struct jsa1212_data *data)
  311. {
  312. int ret;
  313. mutex_lock(&data->lock);
  314. ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
  315. JSA1212_CONF_ALS_MASK |
  316. JSA1212_CONF_PXS_MASK,
  317. JSA1212_CONF_ALS_DISABLE |
  318. JSA1212_CONF_PXS_DISABLE);
  319. if (ret < 0)
  320. dev_err(&data->client->dev, "power off cmd failed\n");
  321. mutex_unlock(&data->lock);
  322. return ret;
  323. }
  324. static int jsa1212_remove(struct i2c_client *client)
  325. {
  326. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  327. struct jsa1212_data *data = iio_priv(indio_dev);
  328. iio_device_unregister(indio_dev);
  329. return jsa1212_power_off(data);
  330. }
  331. #ifdef CONFIG_PM_SLEEP
  332. static int jsa1212_suspend(struct device *dev)
  333. {
  334. struct jsa1212_data *data;
  335. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  336. return jsa1212_power_off(data);
  337. }
  338. static int jsa1212_resume(struct device *dev)
  339. {
  340. int ret = 0;
  341. struct jsa1212_data *data;
  342. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  343. mutex_lock(&data->lock);
  344. if (data->als_en) {
  345. ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
  346. if (ret < 0) {
  347. dev_err(dev, "als resume failed\n");
  348. goto unlock_and_ret;
  349. }
  350. }
  351. if (data->pxs_en) {
  352. ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
  353. if (ret < 0)
  354. dev_err(dev, "pxs resume failed\n");
  355. }
  356. unlock_and_ret:
  357. mutex_unlock(&data->lock);
  358. return ret;
  359. }
  360. static SIMPLE_DEV_PM_OPS(jsa1212_pm_ops, jsa1212_suspend, jsa1212_resume);
  361. #define JSA1212_PM_OPS (&jsa1212_pm_ops)
  362. #else
  363. #define JSA1212_PM_OPS NULL
  364. #endif
  365. static const struct acpi_device_id jsa1212_acpi_match[] = {
  366. {"JSA1212", 0},
  367. { },
  368. };
  369. MODULE_DEVICE_TABLE(acpi, jsa1212_acpi_match);
  370. static const struct i2c_device_id jsa1212_id[] = {
  371. { JSA1212_DRIVER_NAME, 0 },
  372. { }
  373. };
  374. MODULE_DEVICE_TABLE(i2c, jsa1212_id);
  375. static struct i2c_driver jsa1212_driver = {
  376. .driver = {
  377. .name = JSA1212_DRIVER_NAME,
  378. .pm = JSA1212_PM_OPS,
  379. .acpi_match_table = ACPI_PTR(jsa1212_acpi_match),
  380. },
  381. .probe = jsa1212_probe,
  382. .remove = jsa1212_remove,
  383. .id_table = jsa1212_id,
  384. };
  385. module_i2c_driver(jsa1212_driver);
  386. MODULE_AUTHOR("Sathya Kuppuswamy <sathyanarayanan.kuppuswamy@linux.intel.com>");
  387. MODULE_DESCRIPTION("JSA1212 proximity/ambient light sensor driver");
  388. MODULE_LICENSE("GPL v2");