jsa1212.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. .read_raw = &jsa1212_read_raw,
  238. };
  239. static int jsa1212_chip_init(struct jsa1212_data *data)
  240. {
  241. int ret;
  242. ret = regmap_write(data->regmap, JSA1212_CONF_REG,
  243. (JSA1212_CONF_PXS_SLP_50MS |
  244. JSA1212_CONF_IRDR_200MA));
  245. if (ret < 0)
  246. return ret;
  247. ret = regmap_write(data->regmap, JSA1212_INT_REG,
  248. JSA1212_INT_ALS_PRST_4CONV);
  249. if (ret < 0)
  250. return ret;
  251. data->als_rng_idx = JSA1212_ALS_RNG_0_2048;
  252. return 0;
  253. }
  254. static bool jsa1212_is_volatile_reg(struct device *dev, unsigned int reg)
  255. {
  256. switch (reg) {
  257. case JSA1212_PXS_DATA_REG:
  258. case JSA1212_ALS_DT1_REG:
  259. case JSA1212_ALS_DT2_REG:
  260. case JSA1212_INT_REG:
  261. return true;
  262. default:
  263. return false;
  264. }
  265. }
  266. static const struct regmap_config jsa1212_regmap_config = {
  267. .name = JSA1212_REGMAP_NAME,
  268. .reg_bits = 8,
  269. .val_bits = 8,
  270. .max_register = JSA1212_MAX_REG,
  271. .cache_type = REGCACHE_RBTREE,
  272. .volatile_reg = jsa1212_is_volatile_reg,
  273. };
  274. static int jsa1212_probe(struct i2c_client *client,
  275. const struct i2c_device_id *id)
  276. {
  277. struct jsa1212_data *data;
  278. struct iio_dev *indio_dev;
  279. struct regmap *regmap;
  280. int ret;
  281. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  282. if (!indio_dev)
  283. return -ENOMEM;
  284. regmap = devm_regmap_init_i2c(client, &jsa1212_regmap_config);
  285. if (IS_ERR(regmap)) {
  286. dev_err(&client->dev, "Regmap initialization failed.\n");
  287. return PTR_ERR(regmap);
  288. }
  289. data = iio_priv(indio_dev);
  290. i2c_set_clientdata(client, indio_dev);
  291. data->client = client;
  292. data->regmap = regmap;
  293. mutex_init(&data->lock);
  294. ret = jsa1212_chip_init(data);
  295. if (ret < 0)
  296. return ret;
  297. indio_dev->dev.parent = &client->dev;
  298. indio_dev->channels = jsa1212_channels;
  299. indio_dev->num_channels = ARRAY_SIZE(jsa1212_channels);
  300. indio_dev->name = JSA1212_DRIVER_NAME;
  301. indio_dev->modes = INDIO_DIRECT_MODE;
  302. indio_dev->info = &jsa1212_info;
  303. ret = iio_device_register(indio_dev);
  304. if (ret < 0)
  305. dev_err(&client->dev, "%s: register device failed\n", __func__);
  306. return ret;
  307. }
  308. /* power off the device */
  309. static int jsa1212_power_off(struct jsa1212_data *data)
  310. {
  311. int ret;
  312. mutex_lock(&data->lock);
  313. ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
  314. JSA1212_CONF_ALS_MASK |
  315. JSA1212_CONF_PXS_MASK,
  316. JSA1212_CONF_ALS_DISABLE |
  317. JSA1212_CONF_PXS_DISABLE);
  318. if (ret < 0)
  319. dev_err(&data->client->dev, "power off cmd failed\n");
  320. mutex_unlock(&data->lock);
  321. return ret;
  322. }
  323. static int jsa1212_remove(struct i2c_client *client)
  324. {
  325. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  326. struct jsa1212_data *data = iio_priv(indio_dev);
  327. iio_device_unregister(indio_dev);
  328. return jsa1212_power_off(data);
  329. }
  330. #ifdef CONFIG_PM_SLEEP
  331. static int jsa1212_suspend(struct device *dev)
  332. {
  333. struct jsa1212_data *data;
  334. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  335. return jsa1212_power_off(data);
  336. }
  337. static int jsa1212_resume(struct device *dev)
  338. {
  339. int ret = 0;
  340. struct jsa1212_data *data;
  341. data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  342. mutex_lock(&data->lock);
  343. if (data->als_en) {
  344. ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
  345. if (ret < 0) {
  346. dev_err(dev, "als resume failed\n");
  347. goto unlock_and_ret;
  348. }
  349. }
  350. if (data->pxs_en) {
  351. ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
  352. if (ret < 0)
  353. dev_err(dev, "pxs resume failed\n");
  354. }
  355. unlock_and_ret:
  356. mutex_unlock(&data->lock);
  357. return ret;
  358. }
  359. static SIMPLE_DEV_PM_OPS(jsa1212_pm_ops, jsa1212_suspend, jsa1212_resume);
  360. #define JSA1212_PM_OPS (&jsa1212_pm_ops)
  361. #else
  362. #define JSA1212_PM_OPS NULL
  363. #endif
  364. static const struct acpi_device_id jsa1212_acpi_match[] = {
  365. {"JSA1212", 0},
  366. { },
  367. };
  368. MODULE_DEVICE_TABLE(acpi, jsa1212_acpi_match);
  369. static const struct i2c_device_id jsa1212_id[] = {
  370. { JSA1212_DRIVER_NAME, 0 },
  371. { }
  372. };
  373. MODULE_DEVICE_TABLE(i2c, jsa1212_id);
  374. static struct i2c_driver jsa1212_driver = {
  375. .driver = {
  376. .name = JSA1212_DRIVER_NAME,
  377. .pm = JSA1212_PM_OPS,
  378. .acpi_match_table = ACPI_PTR(jsa1212_acpi_match),
  379. },
  380. .probe = jsa1212_probe,
  381. .remove = jsa1212_remove,
  382. .id_table = jsa1212_id,
  383. };
  384. module_i2c_driver(jsa1212_driver);
  385. MODULE_AUTHOR("Sathya Kuppuswamy <sathyanarayanan.kuppuswamy@linux.intel.com>");
  386. MODULE_DESCRIPTION("JSA1212 proximity/ambient light sensor driver");
  387. MODULE_LICENSE("GPL v2");