isl29028.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * IIO driver for the light sensor ISL29028.
  3. * ISL29028 is Concurrent Ambient Light and Proximity Sensor
  4. *
  5. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/i2c.h>
  21. #include <linux/err.h>
  22. #include <linux/mutex.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/regmap.h>
  26. #include <linux/iio/iio.h>
  27. #include <linux/iio/sysfs.h>
  28. #define CONVERSION_TIME_MS 100
  29. #define ISL29028_REG_CONFIGURE 0x01
  30. #define CONFIGURE_ALS_IR_MODE_ALS 0
  31. #define CONFIGURE_ALS_IR_MODE_IR BIT(0)
  32. #define CONFIGURE_ALS_IR_MODE_MASK BIT(0)
  33. #define CONFIGURE_ALS_RANGE_LOW_LUX 0
  34. #define CONFIGURE_ALS_RANGE_HIGH_LUX BIT(1)
  35. #define CONFIGURE_ALS_RANGE_MASK BIT(1)
  36. #define CONFIGURE_ALS_DIS 0
  37. #define CONFIGURE_ALS_EN BIT(2)
  38. #define CONFIGURE_ALS_EN_MASK BIT(2)
  39. #define CONFIGURE_PROX_DRIVE BIT(3)
  40. #define CONFIGURE_PROX_SLP_SH 4
  41. #define CONFIGURE_PROX_SLP_MASK (7 << CONFIGURE_PROX_SLP_SH)
  42. #define CONFIGURE_PROX_EN BIT(7)
  43. #define CONFIGURE_PROX_EN_MASK BIT(7)
  44. #define ISL29028_REG_INTERRUPT 0x02
  45. #define ISL29028_REG_PROX_DATA 0x08
  46. #define ISL29028_REG_ALSIR_L 0x09
  47. #define ISL29028_REG_ALSIR_U 0x0A
  48. #define ISL29028_REG_TEST1_MODE 0x0E
  49. #define ISL29028_REG_TEST2_MODE 0x0F
  50. #define ISL29028_NUM_REGS (ISL29028_REG_TEST2_MODE + 1)
  51. enum als_ir_mode {
  52. MODE_NONE = 0,
  53. MODE_ALS,
  54. MODE_IR
  55. };
  56. struct isl29028_chip {
  57. struct device *dev;
  58. struct mutex lock;
  59. struct regmap *regmap;
  60. unsigned int prox_sampling;
  61. bool enable_prox;
  62. int lux_scale;
  63. int als_ir_mode;
  64. };
  65. static int isl29028_set_proxim_sampling(struct isl29028_chip *chip,
  66. unsigned int sampling)
  67. {
  68. static unsigned int prox_period[] = {800, 400, 200, 100, 75, 50, 12, 0};
  69. int sel;
  70. unsigned int period = DIV_ROUND_UP(1000, sampling);
  71. for (sel = 0; sel < ARRAY_SIZE(prox_period); ++sel) {
  72. if (period >= prox_period[sel])
  73. break;
  74. }
  75. return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  76. CONFIGURE_PROX_SLP_MASK, sel << CONFIGURE_PROX_SLP_SH);
  77. }
  78. static int isl29028_enable_proximity(struct isl29028_chip *chip, bool enable)
  79. {
  80. int ret;
  81. int val = 0;
  82. if (enable)
  83. val = CONFIGURE_PROX_EN;
  84. ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  85. CONFIGURE_PROX_EN_MASK, val);
  86. if (ret < 0)
  87. return ret;
  88. /* Wait for conversion to be complete for first sample */
  89. mdelay(DIV_ROUND_UP(1000, chip->prox_sampling));
  90. return 0;
  91. }
  92. static int isl29028_set_als_scale(struct isl29028_chip *chip, int lux_scale)
  93. {
  94. int val = (lux_scale == 2000) ? CONFIGURE_ALS_RANGE_HIGH_LUX :
  95. CONFIGURE_ALS_RANGE_LOW_LUX;
  96. return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  97. CONFIGURE_ALS_RANGE_MASK, val);
  98. }
  99. static int isl29028_set_als_ir_mode(struct isl29028_chip *chip,
  100. enum als_ir_mode mode)
  101. {
  102. int ret = 0;
  103. switch (mode) {
  104. case MODE_ALS:
  105. ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  106. CONFIGURE_ALS_IR_MODE_MASK, CONFIGURE_ALS_IR_MODE_ALS);
  107. if (ret < 0)
  108. return ret;
  109. ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  110. CONFIGURE_ALS_RANGE_MASK, CONFIGURE_ALS_RANGE_HIGH_LUX);
  111. break;
  112. case MODE_IR:
  113. ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  114. CONFIGURE_ALS_IR_MODE_MASK, CONFIGURE_ALS_IR_MODE_IR);
  115. break;
  116. case MODE_NONE:
  117. return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  118. CONFIGURE_ALS_EN_MASK, CONFIGURE_ALS_DIS);
  119. }
  120. if (ret < 0)
  121. return ret;
  122. /* Enable the ALS/IR */
  123. ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
  124. CONFIGURE_ALS_EN_MASK, CONFIGURE_ALS_EN);
  125. if (ret < 0)
  126. return ret;
  127. /* Need to wait for conversion time if ALS/IR mode enabled */
  128. mdelay(CONVERSION_TIME_MS);
  129. return 0;
  130. }
  131. static int isl29028_read_als_ir(struct isl29028_chip *chip, int *als_ir)
  132. {
  133. unsigned int lsb;
  134. unsigned int msb;
  135. int ret;
  136. ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_L, &lsb);
  137. if (ret < 0) {
  138. dev_err(chip->dev,
  139. "Error in reading register ALSIR_L err %d\n", ret);
  140. return ret;
  141. }
  142. ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_U, &msb);
  143. if (ret < 0) {
  144. dev_err(chip->dev,
  145. "Error in reading register ALSIR_U err %d\n", ret);
  146. return ret;
  147. }
  148. *als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
  149. return 0;
  150. }
  151. static int isl29028_read_proxim(struct isl29028_chip *chip, int *prox)
  152. {
  153. unsigned int data;
  154. int ret;
  155. ret = regmap_read(chip->regmap, ISL29028_REG_PROX_DATA, &data);
  156. if (ret < 0) {
  157. dev_err(chip->dev, "Error in reading register %d, error %d\n",
  158. ISL29028_REG_PROX_DATA, ret);
  159. return ret;
  160. }
  161. *prox = data;
  162. return 0;
  163. }
  164. static int isl29028_proxim_get(struct isl29028_chip *chip, int *prox_data)
  165. {
  166. int ret;
  167. if (!chip->enable_prox) {
  168. ret = isl29028_enable_proximity(chip, true);
  169. if (ret < 0)
  170. return ret;
  171. chip->enable_prox = true;
  172. }
  173. return isl29028_read_proxim(chip, prox_data);
  174. }
  175. static int isl29028_als_get(struct isl29028_chip *chip, int *als_data)
  176. {
  177. int ret;
  178. int als_ir_data;
  179. if (chip->als_ir_mode != MODE_ALS) {
  180. ret = isl29028_set_als_ir_mode(chip, MODE_ALS);
  181. if (ret < 0) {
  182. dev_err(chip->dev,
  183. "Error in enabling ALS mode err %d\n", ret);
  184. return ret;
  185. }
  186. chip->als_ir_mode = MODE_ALS;
  187. }
  188. ret = isl29028_read_als_ir(chip, &als_ir_data);
  189. if (ret < 0)
  190. return ret;
  191. /*
  192. * convert als data count to lux.
  193. * if lux_scale = 125, lux = count * 0.031
  194. * if lux_scale = 2000, lux = count * 0.49
  195. */
  196. if (chip->lux_scale == 125)
  197. als_ir_data = (als_ir_data * 31) / 1000;
  198. else
  199. als_ir_data = (als_ir_data * 49) / 100;
  200. *als_data = als_ir_data;
  201. return 0;
  202. }
  203. static int isl29028_ir_get(struct isl29028_chip *chip, int *ir_data)
  204. {
  205. int ret;
  206. if (chip->als_ir_mode != MODE_IR) {
  207. ret = isl29028_set_als_ir_mode(chip, MODE_IR);
  208. if (ret < 0) {
  209. dev_err(chip->dev,
  210. "Error in enabling IR mode err %d\n", ret);
  211. return ret;
  212. }
  213. chip->als_ir_mode = MODE_IR;
  214. }
  215. return isl29028_read_als_ir(chip, ir_data);
  216. }
  217. /* Channel IO */
  218. static int isl29028_write_raw(struct iio_dev *indio_dev,
  219. struct iio_chan_spec const *chan, int val, int val2, long mask)
  220. {
  221. struct isl29028_chip *chip = iio_priv(indio_dev);
  222. int ret = -EINVAL;
  223. mutex_lock(&chip->lock);
  224. switch (chan->type) {
  225. case IIO_PROXIMITY:
  226. if (mask != IIO_CHAN_INFO_SAMP_FREQ) {
  227. dev_err(chip->dev,
  228. "proximity: mask value 0x%08lx not supported\n",
  229. mask);
  230. break;
  231. }
  232. if (val < 1 || val > 100) {
  233. dev_err(chip->dev,
  234. "Samp_freq %d is not in range[1:100]\n", val);
  235. break;
  236. }
  237. ret = isl29028_set_proxim_sampling(chip, val);
  238. if (ret < 0) {
  239. dev_err(chip->dev,
  240. "Setting proximity samp_freq fail, err %d\n",
  241. ret);
  242. break;
  243. }
  244. chip->prox_sampling = val;
  245. break;
  246. case IIO_LIGHT:
  247. if (mask != IIO_CHAN_INFO_SCALE) {
  248. dev_err(chip->dev,
  249. "light: mask value 0x%08lx not supported\n",
  250. mask);
  251. break;
  252. }
  253. if ((val != 125) && (val != 2000)) {
  254. dev_err(chip->dev,
  255. "lux scale %d is invalid [125, 2000]\n", val);
  256. break;
  257. }
  258. ret = isl29028_set_als_scale(chip, val);
  259. if (ret < 0) {
  260. dev_err(chip->dev,
  261. "Setting lux scale fail with error %d\n", ret);
  262. break;
  263. }
  264. chip->lux_scale = val;
  265. break;
  266. default:
  267. dev_err(chip->dev, "Unsupported channel type\n");
  268. break;
  269. }
  270. mutex_unlock(&chip->lock);
  271. return ret;
  272. }
  273. static int isl29028_read_raw(struct iio_dev *indio_dev,
  274. struct iio_chan_spec const *chan, int *val, int *val2, long mask)
  275. {
  276. struct isl29028_chip *chip = iio_priv(indio_dev);
  277. int ret = -EINVAL;
  278. mutex_lock(&chip->lock);
  279. switch (mask) {
  280. case IIO_CHAN_INFO_RAW:
  281. case IIO_CHAN_INFO_PROCESSED:
  282. switch (chan->type) {
  283. case IIO_LIGHT:
  284. ret = isl29028_als_get(chip, val);
  285. break;
  286. case IIO_INTENSITY:
  287. ret = isl29028_ir_get(chip, val);
  288. break;
  289. case IIO_PROXIMITY:
  290. ret = isl29028_proxim_get(chip, val);
  291. break;
  292. default:
  293. break;
  294. }
  295. if (ret < 0)
  296. break;
  297. ret = IIO_VAL_INT;
  298. break;
  299. case IIO_CHAN_INFO_SAMP_FREQ:
  300. if (chan->type != IIO_PROXIMITY)
  301. break;
  302. *val = chip->prox_sampling;
  303. ret = IIO_VAL_INT;
  304. break;
  305. case IIO_CHAN_INFO_SCALE:
  306. if (chan->type != IIO_LIGHT)
  307. break;
  308. *val = chip->lux_scale;
  309. ret = IIO_VAL_INT;
  310. break;
  311. default:
  312. dev_err(chip->dev, "mask value 0x%08lx not supported\n", mask);
  313. break;
  314. }
  315. mutex_unlock(&chip->lock);
  316. return ret;
  317. }
  318. static IIO_CONST_ATTR(in_proximity_sampling_frequency_available,
  319. "1, 3, 5, 10, 13, 20, 83, 100");
  320. static IIO_CONST_ATTR(in_illuminance_scale_available, "125, 2000");
  321. #define ISL29028_DEV_ATTR(name) (&iio_dev_attr_##name.dev_attr.attr)
  322. #define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
  323. static struct attribute *isl29028_attributes[] = {
  324. ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available),
  325. ISL29028_CONST_ATTR(in_illuminance_scale_available),
  326. NULL,
  327. };
  328. static const struct attribute_group isl29108_group = {
  329. .attrs = isl29028_attributes,
  330. };
  331. static const struct iio_chan_spec isl29028_channels[] = {
  332. {
  333. .type = IIO_LIGHT,
  334. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  335. BIT(IIO_CHAN_INFO_SCALE),
  336. }, {
  337. .type = IIO_INTENSITY,
  338. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  339. }, {
  340. .type = IIO_PROXIMITY,
  341. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  342. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  343. }
  344. };
  345. static const struct iio_info isl29028_info = {
  346. .attrs = &isl29108_group,
  347. .driver_module = THIS_MODULE,
  348. .read_raw = &isl29028_read_raw,
  349. .write_raw = &isl29028_write_raw,
  350. };
  351. static int isl29028_chip_init(struct isl29028_chip *chip)
  352. {
  353. int ret;
  354. chip->enable_prox = false;
  355. chip->prox_sampling = 20;
  356. chip->lux_scale = 2000;
  357. chip->als_ir_mode = MODE_NONE;
  358. ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0);
  359. if (ret < 0) {
  360. dev_err(chip->dev, "%s(): write to reg %d failed, err = %d\n",
  361. __func__, ISL29028_REG_TEST1_MODE, ret);
  362. return ret;
  363. }
  364. ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0);
  365. if (ret < 0) {
  366. dev_err(chip->dev, "%s(): write to reg %d failed, err = %d\n",
  367. __func__, ISL29028_REG_TEST2_MODE, ret);
  368. return ret;
  369. }
  370. ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0);
  371. if (ret < 0) {
  372. dev_err(chip->dev, "%s(): write to reg %d failed, err = %d\n",
  373. __func__, ISL29028_REG_CONFIGURE, ret);
  374. return ret;
  375. }
  376. ret = isl29028_set_proxim_sampling(chip, chip->prox_sampling);
  377. if (ret < 0) {
  378. dev_err(chip->dev, "setting the proximity, err = %d\n",
  379. ret);
  380. return ret;
  381. }
  382. ret = isl29028_set_als_scale(chip, chip->lux_scale);
  383. if (ret < 0)
  384. dev_err(chip->dev,
  385. "setting als scale failed, err = %d\n", ret);
  386. return ret;
  387. }
  388. static bool is_volatile_reg(struct device *dev, unsigned int reg)
  389. {
  390. switch (reg) {
  391. case ISL29028_REG_INTERRUPT:
  392. case ISL29028_REG_PROX_DATA:
  393. case ISL29028_REG_ALSIR_L:
  394. case ISL29028_REG_ALSIR_U:
  395. return true;
  396. default:
  397. return false;
  398. }
  399. }
  400. static const struct regmap_config isl29028_regmap_config = {
  401. .reg_bits = 8,
  402. .val_bits = 8,
  403. .volatile_reg = is_volatile_reg,
  404. .max_register = ISL29028_NUM_REGS - 1,
  405. .num_reg_defaults_raw = ISL29028_NUM_REGS,
  406. .cache_type = REGCACHE_RBTREE,
  407. };
  408. static int isl29028_probe(struct i2c_client *client,
  409. const struct i2c_device_id *id)
  410. {
  411. struct isl29028_chip *chip;
  412. struct iio_dev *indio_dev;
  413. int ret;
  414. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
  415. if (!indio_dev) {
  416. dev_err(&client->dev, "iio allocation fails\n");
  417. return -ENOMEM;
  418. }
  419. chip = iio_priv(indio_dev);
  420. i2c_set_clientdata(client, indio_dev);
  421. chip->dev = &client->dev;
  422. mutex_init(&chip->lock);
  423. chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config);
  424. if (IS_ERR(chip->regmap)) {
  425. ret = PTR_ERR(chip->regmap);
  426. dev_err(chip->dev, "regmap initialization failed: %d\n", ret);
  427. return ret;
  428. }
  429. ret = isl29028_chip_init(chip);
  430. if (ret < 0) {
  431. dev_err(chip->dev, "chip initialization failed: %d\n", ret);
  432. return ret;
  433. }
  434. indio_dev->info = &isl29028_info;
  435. indio_dev->channels = isl29028_channels;
  436. indio_dev->num_channels = ARRAY_SIZE(isl29028_channels);
  437. indio_dev->name = id->name;
  438. indio_dev->dev.parent = &client->dev;
  439. indio_dev->modes = INDIO_DIRECT_MODE;
  440. ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
  441. if (ret < 0) {
  442. dev_err(chip->dev, "iio registration fails with error %d\n",
  443. ret);
  444. return ret;
  445. }
  446. return 0;
  447. }
  448. static const struct i2c_device_id isl29028_id[] = {
  449. {"isl29028", 0},
  450. {}
  451. };
  452. MODULE_DEVICE_TABLE(i2c, isl29028_id);
  453. static const struct of_device_id isl29028_of_match[] = {
  454. { .compatible = "isl,isl29028", }, /* for backward compat., don't use */
  455. { .compatible = "isil,isl29028", },
  456. { },
  457. };
  458. MODULE_DEVICE_TABLE(of, isl29028_of_match);
  459. static struct i2c_driver isl29028_driver = {
  460. .class = I2C_CLASS_HWMON,
  461. .driver = {
  462. .name = "isl29028",
  463. .of_match_table = isl29028_of_match,
  464. },
  465. .probe = isl29028_probe,
  466. .id_table = isl29028_id,
  467. };
  468. module_i2c_driver(isl29028_driver);
  469. MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver");
  470. MODULE_LICENSE("GPL v2");
  471. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");