isl29501.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * isl29501.c: ISL29501 Time of Flight sensor driver.
  4. *
  5. * Copyright (C) 2018
  6. * Author: Mathieu Othacehe <m.othacehe@gmail.com>
  7. *
  8. * 7-bit I2C slave address: 0x57
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/err.h>
  14. #include <linux/of_device.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/sysfs.h>
  17. #include <linux/iio/trigger_consumer.h>
  18. #include <linux/iio/buffer.h>
  19. #include <linux/iio/triggered_buffer.h>
  20. /* Control, setting and status registers */
  21. #define ISL29501_DEVICE_ID 0x00
  22. #define ISL29501_ID 0x0A
  23. /* Sampling control registers */
  24. #define ISL29501_INTEGRATION_PERIOD 0x10
  25. #define ISL29501_SAMPLE_PERIOD 0x11
  26. /* Closed loop calibration registers */
  27. #define ISL29501_CROSSTALK_I_MSB 0x24
  28. #define ISL29501_CROSSTALK_I_LSB 0x25
  29. #define ISL29501_CROSSTALK_I_EXPONENT 0x26
  30. #define ISL29501_CROSSTALK_Q_MSB 0x27
  31. #define ISL29501_CROSSTALK_Q_LSB 0x28
  32. #define ISL29501_CROSSTALK_Q_EXPONENT 0x29
  33. #define ISL29501_CROSSTALK_GAIN_MSB 0x2A
  34. #define ISL29501_CROSSTALK_GAIN_LSB 0x2B
  35. #define ISL29501_MAGNITUDE_REF_EXP 0x2C
  36. #define ISL29501_MAGNITUDE_REF_MSB 0x2D
  37. #define ISL29501_MAGNITUDE_REF_LSB 0x2E
  38. #define ISL29501_PHASE_OFFSET_MSB 0x2F
  39. #define ISL29501_PHASE_OFFSET_LSB 0x30
  40. /* Analog control registers */
  41. #define ISL29501_DRIVER_RANGE 0x90
  42. #define ISL29501_EMITTER_DAC 0x91
  43. #define ISL29501_COMMAND_REGISTER 0xB0
  44. /* Commands */
  45. #define ISL29501_EMUL_SAMPLE_START_PIN 0x49
  46. #define ISL29501_RESET_ALL_REGISTERS 0xD7
  47. #define ISL29501_RESET_INT_SM 0xD1
  48. /* Ambiant light and temperature corrections */
  49. #define ISL29501_TEMP_REFERENCE 0x31
  50. #define ISL29501_PHASE_EXPONENT 0x33
  51. #define ISL29501_TEMP_COEFF_A 0x34
  52. #define ISL29501_TEMP_COEFF_B 0x39
  53. #define ISL29501_AMBIANT_COEFF_A 0x36
  54. #define ISL29501_AMBIANT_COEFF_B 0x3B
  55. /* Data output registers */
  56. #define ISL29501_DISTANCE_MSB_DATA 0xD1
  57. #define ISL29501_DISTANCE_LSB_DATA 0xD2
  58. #define ISL29501_PRECISION_MSB 0xD3
  59. #define ISL29501_PRECISION_LSB 0xD4
  60. #define ISL29501_MAGNITUDE_EXPONENT 0xD5
  61. #define ISL29501_MAGNITUDE_MSB 0xD6
  62. #define ISL29501_MAGNITUDE_LSB 0xD7
  63. #define ISL29501_PHASE_MSB 0xD8
  64. #define ISL29501_PHASE_LSB 0xD9
  65. #define ISL29501_I_RAW_EXPONENT 0xDA
  66. #define ISL29501_I_RAW_MSB 0xDB
  67. #define ISL29501_I_RAW_LSB 0xDC
  68. #define ISL29501_Q_RAW_EXPONENT 0xDD
  69. #define ISL29501_Q_RAW_MSB 0xDE
  70. #define ISL29501_Q_RAW_LSB 0xDF
  71. #define ISL29501_DIE_TEMPERATURE 0xE2
  72. #define ISL29501_AMBIENT_LIGHT 0xE3
  73. #define ISL29501_GAIN_MSB 0xE6
  74. #define ISL29501_GAIN_LSB 0xE7
  75. #define ISL29501_MAX_EXP_VAL 15
  76. #define ISL29501_INT_TIME_AVAILABLE \
  77. "0.00007 0.00014 0.00028 0.00057 0.00114 " \
  78. "0.00228 0.00455 0.00910 0.01820 0.03640 " \
  79. "0.07281 0.14561"
  80. #define ISL29501_CURRENT_SCALE_AVAILABLE \
  81. "0.0039 0.0078 0.0118 0.0157 0.0196 " \
  82. "0.0235 0.0275 0.0314 0.0352 0.0392 " \
  83. "0.0431 0.0471 0.0510 0.0549 0.0588"
  84. enum isl29501_correction_coeff {
  85. COEFF_TEMP_A,
  86. COEFF_TEMP_B,
  87. COEFF_LIGHT_A,
  88. COEFF_LIGHT_B,
  89. COEFF_MAX,
  90. };
  91. struct isl29501_private {
  92. struct i2c_client *client;
  93. struct mutex lock;
  94. /* Exact representation of correction coefficients. */
  95. unsigned int shadow_coeffs[COEFF_MAX];
  96. };
  97. enum isl29501_register_name {
  98. REG_DISTANCE,
  99. REG_PHASE,
  100. REG_TEMPERATURE,
  101. REG_AMBIENT_LIGHT,
  102. REG_GAIN,
  103. REG_GAIN_BIAS,
  104. REG_PHASE_EXP,
  105. REG_CALIB_PHASE_TEMP_A,
  106. REG_CALIB_PHASE_TEMP_B,
  107. REG_CALIB_PHASE_LIGHT_A,
  108. REG_CALIB_PHASE_LIGHT_B,
  109. REG_DISTANCE_BIAS,
  110. REG_TEMPERATURE_BIAS,
  111. REG_INT_TIME,
  112. REG_SAMPLE_TIME,
  113. REG_DRIVER_RANGE,
  114. REG_EMITTER_DAC,
  115. };
  116. struct isl29501_register_desc {
  117. u8 msb;
  118. u8 lsb;
  119. };
  120. static const struct isl29501_register_desc isl29501_registers[] = {
  121. [REG_DISTANCE] = {
  122. .msb = ISL29501_DISTANCE_MSB_DATA,
  123. .lsb = ISL29501_DISTANCE_LSB_DATA,
  124. },
  125. [REG_PHASE] = {
  126. .msb = ISL29501_PHASE_MSB,
  127. .lsb = ISL29501_PHASE_LSB,
  128. },
  129. [REG_TEMPERATURE] = {
  130. .lsb = ISL29501_DIE_TEMPERATURE,
  131. },
  132. [REG_AMBIENT_LIGHT] = {
  133. .lsb = ISL29501_AMBIENT_LIGHT,
  134. },
  135. [REG_GAIN] = {
  136. .msb = ISL29501_GAIN_MSB,
  137. .lsb = ISL29501_GAIN_LSB,
  138. },
  139. [REG_GAIN_BIAS] = {
  140. .msb = ISL29501_CROSSTALK_GAIN_MSB,
  141. .lsb = ISL29501_CROSSTALK_GAIN_LSB,
  142. },
  143. [REG_PHASE_EXP] = {
  144. .lsb = ISL29501_PHASE_EXPONENT,
  145. },
  146. [REG_CALIB_PHASE_TEMP_A] = {
  147. .lsb = ISL29501_TEMP_COEFF_A,
  148. },
  149. [REG_CALIB_PHASE_TEMP_B] = {
  150. .lsb = ISL29501_TEMP_COEFF_B,
  151. },
  152. [REG_CALIB_PHASE_LIGHT_A] = {
  153. .lsb = ISL29501_AMBIANT_COEFF_A,
  154. },
  155. [REG_CALIB_PHASE_LIGHT_B] = {
  156. .lsb = ISL29501_AMBIANT_COEFF_B,
  157. },
  158. [REG_DISTANCE_BIAS] = {
  159. .msb = ISL29501_PHASE_OFFSET_MSB,
  160. .lsb = ISL29501_PHASE_OFFSET_LSB,
  161. },
  162. [REG_TEMPERATURE_BIAS] = {
  163. .lsb = ISL29501_TEMP_REFERENCE,
  164. },
  165. [REG_INT_TIME] = {
  166. .lsb = ISL29501_INTEGRATION_PERIOD,
  167. },
  168. [REG_SAMPLE_TIME] = {
  169. .lsb = ISL29501_SAMPLE_PERIOD,
  170. },
  171. [REG_DRIVER_RANGE] = {
  172. .lsb = ISL29501_DRIVER_RANGE,
  173. },
  174. [REG_EMITTER_DAC] = {
  175. .lsb = ISL29501_EMITTER_DAC,
  176. },
  177. };
  178. static int isl29501_register_read(struct isl29501_private *isl29501,
  179. enum isl29501_register_name name,
  180. u32 *val)
  181. {
  182. const struct isl29501_register_desc *reg = &isl29501_registers[name];
  183. u8 msb = 0, lsb = 0;
  184. s32 ret;
  185. mutex_lock(&isl29501->lock);
  186. if (reg->msb) {
  187. ret = i2c_smbus_read_byte_data(isl29501->client, reg->msb);
  188. if (ret < 0)
  189. goto err;
  190. msb = ret;
  191. }
  192. if (reg->lsb) {
  193. ret = i2c_smbus_read_byte_data(isl29501->client, reg->lsb);
  194. if (ret < 0)
  195. goto err;
  196. lsb = ret;
  197. }
  198. mutex_unlock(&isl29501->lock);
  199. *val = (msb << 8) + lsb;
  200. return 0;
  201. err:
  202. mutex_unlock(&isl29501->lock);
  203. return ret;
  204. }
  205. static u32 isl29501_register_write(struct isl29501_private *isl29501,
  206. enum isl29501_register_name name,
  207. u32 value)
  208. {
  209. const struct isl29501_register_desc *reg = &isl29501_registers[name];
  210. u8 msb, lsb;
  211. int ret;
  212. if (!reg->msb && value > U8_MAX)
  213. return -ERANGE;
  214. if (value > U16_MAX)
  215. return -ERANGE;
  216. if (!reg->msb) {
  217. lsb = value & 0xFF;
  218. } else {
  219. msb = (value >> 8) & 0xFF;
  220. lsb = value & 0xFF;
  221. }
  222. mutex_lock(&isl29501->lock);
  223. if (reg->msb) {
  224. ret = i2c_smbus_write_byte_data(isl29501->client,
  225. reg->msb, msb);
  226. if (ret < 0)
  227. goto err;
  228. }
  229. ret = i2c_smbus_write_byte_data(isl29501->client, reg->lsb, lsb);
  230. err:
  231. mutex_unlock(&isl29501->lock);
  232. return ret;
  233. }
  234. static ssize_t isl29501_read_ext(struct iio_dev *indio_dev,
  235. uintptr_t private,
  236. const struct iio_chan_spec *chan,
  237. char *buf)
  238. {
  239. struct isl29501_private *isl29501 = iio_priv(indio_dev);
  240. enum isl29501_register_name reg = private;
  241. int ret;
  242. u32 value, gain, coeff, exp;
  243. switch (reg) {
  244. case REG_GAIN:
  245. case REG_GAIN_BIAS:
  246. ret = isl29501_register_read(isl29501, reg, &gain);
  247. if (ret < 0)
  248. return ret;
  249. value = gain;
  250. break;
  251. case REG_CALIB_PHASE_TEMP_A:
  252. case REG_CALIB_PHASE_TEMP_B:
  253. case REG_CALIB_PHASE_LIGHT_A:
  254. case REG_CALIB_PHASE_LIGHT_B:
  255. ret = isl29501_register_read(isl29501, REG_PHASE_EXP, &exp);
  256. if (ret < 0)
  257. return ret;
  258. ret = isl29501_register_read(isl29501, reg, &coeff);
  259. if (ret < 0)
  260. return ret;
  261. value = coeff << exp;
  262. break;
  263. default:
  264. return -EINVAL;
  265. }
  266. return sprintf(buf, "%u\n", value);
  267. }
  268. static int isl29501_set_shadow_coeff(struct isl29501_private *isl29501,
  269. enum isl29501_register_name reg,
  270. unsigned int val)
  271. {
  272. enum isl29501_correction_coeff coeff;
  273. switch (reg) {
  274. case REG_CALIB_PHASE_TEMP_A:
  275. coeff = COEFF_TEMP_A;
  276. break;
  277. case REG_CALIB_PHASE_TEMP_B:
  278. coeff = COEFF_TEMP_B;
  279. break;
  280. case REG_CALIB_PHASE_LIGHT_A:
  281. coeff = COEFF_LIGHT_A;
  282. break;
  283. case REG_CALIB_PHASE_LIGHT_B:
  284. coeff = COEFF_LIGHT_B;
  285. break;
  286. default:
  287. return -EINVAL;
  288. }
  289. isl29501->shadow_coeffs[coeff] = val;
  290. return 0;
  291. }
  292. static int isl29501_write_coeff(struct isl29501_private *isl29501,
  293. enum isl29501_correction_coeff coeff,
  294. int val)
  295. {
  296. enum isl29501_register_name reg;
  297. switch (coeff) {
  298. case COEFF_TEMP_A:
  299. reg = REG_CALIB_PHASE_TEMP_A;
  300. break;
  301. case COEFF_TEMP_B:
  302. reg = REG_CALIB_PHASE_TEMP_B;
  303. break;
  304. case COEFF_LIGHT_A:
  305. reg = REG_CALIB_PHASE_LIGHT_A;
  306. break;
  307. case COEFF_LIGHT_B:
  308. reg = REG_CALIB_PHASE_LIGHT_B;
  309. break;
  310. default:
  311. return -EINVAL;
  312. }
  313. return isl29501_register_write(isl29501, reg, val);
  314. }
  315. static unsigned int isl29501_find_corr_exp(unsigned int val,
  316. unsigned int max_exp,
  317. unsigned int max_mantissa)
  318. {
  319. unsigned int exp = 1;
  320. /*
  321. * Correction coefficients are represented under
  322. * mantissa * 2^exponent form, where mantissa and exponent
  323. * are stored in two separate registers of the sensor.
  324. *
  325. * Compute and return the lowest exponent such as:
  326. * mantissa = value / 2^exponent
  327. *
  328. * where mantissa < max_mantissa.
  329. */
  330. if (val <= max_mantissa)
  331. return 0;
  332. while ((val >> exp) > max_mantissa) {
  333. exp++;
  334. if (exp > max_exp)
  335. return max_exp;
  336. }
  337. return exp;
  338. }
  339. static ssize_t isl29501_write_ext(struct iio_dev *indio_dev,
  340. uintptr_t private,
  341. const struct iio_chan_spec *chan,
  342. const char *buf, size_t len)
  343. {
  344. struct isl29501_private *isl29501 = iio_priv(indio_dev);
  345. enum isl29501_register_name reg = private;
  346. unsigned int val;
  347. int max_exp = 0;
  348. int ret;
  349. int i;
  350. ret = kstrtouint(buf, 10, &val);
  351. if (ret)
  352. return ret;
  353. switch (reg) {
  354. case REG_GAIN_BIAS:
  355. if (val > U16_MAX)
  356. return -ERANGE;
  357. ret = isl29501_register_write(isl29501, reg, val);
  358. if (ret < 0)
  359. return ret;
  360. break;
  361. case REG_CALIB_PHASE_TEMP_A:
  362. case REG_CALIB_PHASE_TEMP_B:
  363. case REG_CALIB_PHASE_LIGHT_A:
  364. case REG_CALIB_PHASE_LIGHT_B:
  365. if (val > (U8_MAX << ISL29501_MAX_EXP_VAL))
  366. return -ERANGE;
  367. /* Store the correction coefficient under its exact form. */
  368. ret = isl29501_set_shadow_coeff(isl29501, reg, val);
  369. if (ret < 0)
  370. return ret;
  371. /*
  372. * Find the highest exponent needed to represent
  373. * correction coefficients.
  374. */
  375. for (i = 0; i < COEFF_MAX; i++) {
  376. int corr;
  377. int corr_exp;
  378. corr = isl29501->shadow_coeffs[i];
  379. corr_exp = isl29501_find_corr_exp(corr,
  380. ISL29501_MAX_EXP_VAL,
  381. U8_MAX / 2);
  382. dev_dbg(&isl29501->client->dev,
  383. "found exp of corr(%d) = %d\n", corr, corr_exp);
  384. max_exp = max(max_exp, corr_exp);
  385. }
  386. /*
  387. * Represent every correction coefficient under
  388. * mantissa * 2^max_exponent form and force the
  389. * writing of those coefficients on the sensor.
  390. */
  391. for (i = 0; i < COEFF_MAX; i++) {
  392. int corr;
  393. int mantissa;
  394. corr = isl29501->shadow_coeffs[i];
  395. if (!corr)
  396. continue;
  397. mantissa = corr >> max_exp;
  398. ret = isl29501_write_coeff(isl29501, i, mantissa);
  399. if (ret < 0)
  400. return ret;
  401. }
  402. ret = isl29501_register_write(isl29501, REG_PHASE_EXP, max_exp);
  403. if (ret < 0)
  404. return ret;
  405. break;
  406. default:
  407. return -EINVAL;
  408. }
  409. return len;
  410. }
  411. #define _ISL29501_EXT_INFO(_name, _ident) { \
  412. .name = _name, \
  413. .read = isl29501_read_ext, \
  414. .write = isl29501_write_ext, \
  415. .private = _ident, \
  416. .shared = IIO_SEPARATE, \
  417. }
  418. static const struct iio_chan_spec_ext_info isl29501_ext_info[] = {
  419. _ISL29501_EXT_INFO("agc_gain", REG_GAIN),
  420. _ISL29501_EXT_INFO("agc_gain_bias", REG_GAIN_BIAS),
  421. _ISL29501_EXT_INFO("calib_phase_temp_a", REG_CALIB_PHASE_TEMP_A),
  422. _ISL29501_EXT_INFO("calib_phase_temp_b", REG_CALIB_PHASE_TEMP_B),
  423. _ISL29501_EXT_INFO("calib_phase_light_a", REG_CALIB_PHASE_LIGHT_A),
  424. _ISL29501_EXT_INFO("calib_phase_light_b", REG_CALIB_PHASE_LIGHT_B),
  425. { },
  426. };
  427. #define ISL29501_DISTANCE_SCAN_INDEX 0
  428. #define ISL29501_TIMESTAMP_SCAN_INDEX 1
  429. static const struct iio_chan_spec isl29501_channels[] = {
  430. {
  431. .type = IIO_PROXIMITY,
  432. .scan_index = ISL29501_DISTANCE_SCAN_INDEX,
  433. .info_mask_separate =
  434. BIT(IIO_CHAN_INFO_RAW) |
  435. BIT(IIO_CHAN_INFO_SCALE) |
  436. BIT(IIO_CHAN_INFO_CALIBBIAS),
  437. .scan_type = {
  438. .sign = 'u',
  439. .realbits = 16,
  440. .storagebits = 16,
  441. .endianness = IIO_CPU,
  442. },
  443. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
  444. BIT(IIO_CHAN_INFO_SAMP_FREQ),
  445. .ext_info = isl29501_ext_info,
  446. },
  447. {
  448. .type = IIO_PHASE,
  449. .scan_index = -1,
  450. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  451. BIT(IIO_CHAN_INFO_SCALE),
  452. },
  453. {
  454. .type = IIO_CURRENT,
  455. .scan_index = -1,
  456. .output = 1,
  457. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  458. BIT(IIO_CHAN_INFO_SCALE),
  459. },
  460. {
  461. .type = IIO_TEMP,
  462. .scan_index = -1,
  463. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  464. BIT(IIO_CHAN_INFO_SCALE) |
  465. BIT(IIO_CHAN_INFO_CALIBBIAS),
  466. },
  467. {
  468. .type = IIO_INTENSITY,
  469. .scan_index = -1,
  470. .modified = 1,
  471. .channel2 = IIO_MOD_LIGHT_CLEAR,
  472. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  473. BIT(IIO_CHAN_INFO_SCALE),
  474. },
  475. IIO_CHAN_SOFT_TIMESTAMP(ISL29501_TIMESTAMP_SCAN_INDEX),
  476. };
  477. static int isl29501_reset_registers(struct isl29501_private *isl29501)
  478. {
  479. int ret;
  480. ret = i2c_smbus_write_byte_data(isl29501->client,
  481. ISL29501_COMMAND_REGISTER,
  482. ISL29501_RESET_ALL_REGISTERS);
  483. if (ret < 0) {
  484. dev_err(&isl29501->client->dev,
  485. "cannot reset registers %d\n", ret);
  486. return ret;
  487. }
  488. ret = i2c_smbus_write_byte_data(isl29501->client,
  489. ISL29501_COMMAND_REGISTER,
  490. ISL29501_RESET_INT_SM);
  491. if (ret < 0)
  492. dev_err(&isl29501->client->dev,
  493. "cannot reset state machine %d\n", ret);
  494. return ret;
  495. }
  496. static int isl29501_begin_acquisition(struct isl29501_private *isl29501)
  497. {
  498. int ret;
  499. ret = i2c_smbus_write_byte_data(isl29501->client,
  500. ISL29501_COMMAND_REGISTER,
  501. ISL29501_EMUL_SAMPLE_START_PIN);
  502. if (ret < 0)
  503. dev_err(&isl29501->client->dev,
  504. "cannot begin acquisition %d\n", ret);
  505. return ret;
  506. }
  507. static IIO_CONST_ATTR_INT_TIME_AVAIL(ISL29501_INT_TIME_AVAILABLE);
  508. static IIO_CONST_ATTR(out_current_scale_available,
  509. ISL29501_CURRENT_SCALE_AVAILABLE);
  510. static struct attribute *isl29501_attributes[] = {
  511. &iio_const_attr_integration_time_available.dev_attr.attr,
  512. &iio_const_attr_out_current_scale_available.dev_attr.attr,
  513. NULL
  514. };
  515. static const struct attribute_group isl29501_attribute_group = {
  516. .attrs = isl29501_attributes,
  517. };
  518. static const int isl29501_current_scale_table[][2] = {
  519. {0, 3900}, {0, 7800}, {0, 11800}, {0, 15700},
  520. {0, 19600}, {0, 23500}, {0, 27500}, {0, 31400},
  521. {0, 35200}, {0, 39200}, {0, 43100}, {0, 47100},
  522. {0, 51000}, {0, 54900}, {0, 58800},
  523. };
  524. static const int isl29501_int_time[][2] = {
  525. {0, 70}, /* 0.07 ms */
  526. {0, 140}, /* 0.14 ms */
  527. {0, 280}, /* 0.28 ms */
  528. {0, 570}, /* 0.57 ms */
  529. {0, 1140}, /* 1.14 ms */
  530. {0, 2280}, /* 2.28 ms */
  531. {0, 4550}, /* 4.55 ms */
  532. {0, 9100}, /* 9.11 ms */
  533. {0, 18200}, /* 18.2 ms */
  534. {0, 36400}, /* 36.4 ms */
  535. {0, 72810}, /* 72.81 ms */
  536. {0, 145610} /* 145.28 ms */
  537. };
  538. static int isl29501_get_raw(struct isl29501_private *isl29501,
  539. const struct iio_chan_spec *chan,
  540. int *raw)
  541. {
  542. int ret;
  543. switch (chan->type) {
  544. case IIO_PROXIMITY:
  545. ret = isl29501_register_read(isl29501, REG_DISTANCE, raw);
  546. if (ret < 0)
  547. return ret;
  548. return IIO_VAL_INT;
  549. case IIO_INTENSITY:
  550. ret = isl29501_register_read(isl29501,
  551. REG_AMBIENT_LIGHT,
  552. raw);
  553. if (ret < 0)
  554. return ret;
  555. return IIO_VAL_INT;
  556. case IIO_PHASE:
  557. ret = isl29501_register_read(isl29501, REG_PHASE, raw);
  558. if (ret < 0)
  559. return ret;
  560. return IIO_VAL_INT;
  561. case IIO_CURRENT:
  562. ret = isl29501_register_read(isl29501, REG_EMITTER_DAC, raw);
  563. if (ret < 0)
  564. return ret;
  565. return IIO_VAL_INT;
  566. case IIO_TEMP:
  567. ret = isl29501_register_read(isl29501, REG_TEMPERATURE, raw);
  568. if (ret < 0)
  569. return ret;
  570. return IIO_VAL_INT;
  571. default:
  572. return -EINVAL;
  573. }
  574. }
  575. static int isl29501_get_scale(struct isl29501_private *isl29501,
  576. const struct iio_chan_spec *chan,
  577. int *val, int *val2)
  578. {
  579. int ret;
  580. u32 current_scale;
  581. switch (chan->type) {
  582. case IIO_PROXIMITY:
  583. /* distance = raw_distance * 33.31 / 65536 (m) */
  584. *val = 3331;
  585. *val2 = 6553600;
  586. return IIO_VAL_FRACTIONAL;
  587. case IIO_PHASE:
  588. /* phase = raw_phase * 2pi / 65536 (rad) */
  589. *val = 0;
  590. *val2 = 95874;
  591. return IIO_VAL_INT_PLUS_NANO;
  592. case IIO_INTENSITY:
  593. /* light = raw_light * 35 / 10000 (mA) */
  594. *val = 35;
  595. *val2 = 10000;
  596. return IIO_VAL_FRACTIONAL;
  597. case IIO_CURRENT:
  598. ret = isl29501_register_read(isl29501,
  599. REG_DRIVER_RANGE,
  600. &current_scale);
  601. if (ret < 0)
  602. return ret;
  603. if (current_scale > ARRAY_SIZE(isl29501_current_scale_table))
  604. return -EINVAL;
  605. if (!current_scale) {
  606. *val = 0;
  607. *val2 = 0;
  608. return IIO_VAL_INT;
  609. }
  610. *val = isl29501_current_scale_table[current_scale - 1][0];
  611. *val2 = isl29501_current_scale_table[current_scale - 1][1];
  612. return IIO_VAL_INT_PLUS_MICRO;
  613. case IIO_TEMP:
  614. /* temperature = raw_temperature * 125 / 100000 (milli °C) */
  615. *val = 125;
  616. *val2 = 100000;
  617. return IIO_VAL_FRACTIONAL;
  618. default:
  619. return -EINVAL;
  620. }
  621. }
  622. static int isl29501_get_calibbias(struct isl29501_private *isl29501,
  623. const struct iio_chan_spec *chan,
  624. int *bias)
  625. {
  626. switch (chan->type) {
  627. case IIO_PROXIMITY:
  628. return isl29501_register_read(isl29501,
  629. REG_DISTANCE_BIAS,
  630. bias);
  631. case IIO_TEMP:
  632. return isl29501_register_read(isl29501,
  633. REG_TEMPERATURE_BIAS,
  634. bias);
  635. default:
  636. return -EINVAL;
  637. }
  638. }
  639. static int isl29501_get_inttime(struct isl29501_private *isl29501,
  640. int *val, int *val2)
  641. {
  642. int ret;
  643. u32 inttime;
  644. ret = isl29501_register_read(isl29501, REG_INT_TIME, &inttime);
  645. if (ret < 0)
  646. return ret;
  647. if (inttime >= ARRAY_SIZE(isl29501_int_time))
  648. return -EINVAL;
  649. *val = isl29501_int_time[inttime][0];
  650. *val2 = isl29501_int_time[inttime][1];
  651. return IIO_VAL_INT_PLUS_MICRO;
  652. }
  653. static int isl29501_get_freq(struct isl29501_private *isl29501,
  654. int *val, int *val2)
  655. {
  656. int ret;
  657. int sample_time;
  658. unsigned long long freq;
  659. u32 temp;
  660. ret = isl29501_register_read(isl29501, REG_SAMPLE_TIME, &sample_time);
  661. if (ret < 0)
  662. return ret;
  663. /* freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */
  664. freq = 1000000ULL * 1000000ULL;
  665. do_div(freq, 450 * (sample_time + 1));
  666. temp = do_div(freq, 1000000);
  667. *val = freq;
  668. *val2 = temp;
  669. return IIO_VAL_INT_PLUS_MICRO;
  670. }
  671. static int isl29501_read_raw(struct iio_dev *indio_dev,
  672. struct iio_chan_spec const *chan, int *val,
  673. int *val2, long mask)
  674. {
  675. struct isl29501_private *isl29501 = iio_priv(indio_dev);
  676. switch (mask) {
  677. case IIO_CHAN_INFO_RAW:
  678. return isl29501_get_raw(isl29501, chan, val);
  679. case IIO_CHAN_INFO_SCALE:
  680. return isl29501_get_scale(isl29501, chan, val, val2);
  681. case IIO_CHAN_INFO_INT_TIME:
  682. return isl29501_get_inttime(isl29501, val, val2);
  683. case IIO_CHAN_INFO_SAMP_FREQ:
  684. return isl29501_get_freq(isl29501, val, val2);
  685. case IIO_CHAN_INFO_CALIBBIAS:
  686. return isl29501_get_calibbias(isl29501, chan, val);
  687. default:
  688. return -EINVAL;
  689. }
  690. }
  691. static int isl29501_set_raw(struct isl29501_private *isl29501,
  692. const struct iio_chan_spec *chan,
  693. int raw)
  694. {
  695. switch (chan->type) {
  696. case IIO_CURRENT:
  697. return isl29501_register_write(isl29501, REG_EMITTER_DAC, raw);
  698. default:
  699. return -EINVAL;
  700. }
  701. }
  702. static int isl29501_set_inttime(struct isl29501_private *isl29501,
  703. int val, int val2)
  704. {
  705. int i;
  706. for (i = 0; i < ARRAY_SIZE(isl29501_int_time); i++) {
  707. if (isl29501_int_time[i][0] == val &&
  708. isl29501_int_time[i][1] == val2) {
  709. return isl29501_register_write(isl29501,
  710. REG_INT_TIME,
  711. i);
  712. }
  713. }
  714. return -EINVAL;
  715. }
  716. static int isl29501_set_scale(struct isl29501_private *isl29501,
  717. const struct iio_chan_spec *chan,
  718. int val, int val2)
  719. {
  720. int i;
  721. if (chan->type != IIO_CURRENT)
  722. return -EINVAL;
  723. for (i = 0; i < ARRAY_SIZE(isl29501_current_scale_table); i++) {
  724. if (isl29501_current_scale_table[i][0] == val &&
  725. isl29501_current_scale_table[i][1] == val2) {
  726. return isl29501_register_write(isl29501,
  727. REG_DRIVER_RANGE,
  728. i + 1);
  729. }
  730. }
  731. return -EINVAL;
  732. }
  733. static int isl29501_set_calibbias(struct isl29501_private *isl29501,
  734. const struct iio_chan_spec *chan,
  735. int bias)
  736. {
  737. switch (chan->type) {
  738. case IIO_PROXIMITY:
  739. return isl29501_register_write(isl29501,
  740. REG_DISTANCE_BIAS,
  741. bias);
  742. case IIO_TEMP:
  743. return isl29501_register_write(isl29501,
  744. REG_TEMPERATURE_BIAS,
  745. bias);
  746. default:
  747. return -EINVAL;
  748. }
  749. }
  750. static int isl29501_set_freq(struct isl29501_private *isl29501,
  751. int val, int val2)
  752. {
  753. int freq;
  754. unsigned long long sample_time;
  755. /* sample_freq = 1 / (0.000450 * (sample_time + 1) * 10^-6) */
  756. freq = val * 1000000 + val2 % 1000000;
  757. sample_time = 2222ULL * 1000000ULL;
  758. do_div(sample_time, freq);
  759. sample_time -= 1;
  760. if (sample_time > 255)
  761. return -ERANGE;
  762. return isl29501_register_write(isl29501, REG_SAMPLE_TIME, sample_time);
  763. }
  764. static int isl29501_write_raw(struct iio_dev *indio_dev,
  765. struct iio_chan_spec const *chan,
  766. int val, int val2, long mask)
  767. {
  768. struct isl29501_private *isl29501 = iio_priv(indio_dev);
  769. switch (mask) {
  770. case IIO_CHAN_INFO_RAW:
  771. return isl29501_set_raw(isl29501, chan, val);
  772. case IIO_CHAN_INFO_INT_TIME:
  773. return isl29501_set_inttime(isl29501, val, val2);
  774. case IIO_CHAN_INFO_SAMP_FREQ:
  775. return isl29501_set_freq(isl29501, val, val2);
  776. case IIO_CHAN_INFO_SCALE:
  777. return isl29501_set_scale(isl29501, chan, val, val2);
  778. case IIO_CHAN_INFO_CALIBBIAS:
  779. return isl29501_set_calibbias(isl29501, chan, val);
  780. default:
  781. return -EINVAL;
  782. }
  783. }
  784. static const struct iio_info isl29501_info = {
  785. .read_raw = &isl29501_read_raw,
  786. .write_raw = &isl29501_write_raw,
  787. .attrs = &isl29501_attribute_group,
  788. };
  789. static int isl29501_init_chip(struct isl29501_private *isl29501)
  790. {
  791. int ret;
  792. ret = i2c_smbus_read_byte_data(isl29501->client, ISL29501_DEVICE_ID);
  793. if (ret < 0) {
  794. dev_err(&isl29501->client->dev, "Error reading device id\n");
  795. return ret;
  796. }
  797. if (ret != ISL29501_ID) {
  798. dev_err(&isl29501->client->dev,
  799. "Wrong chip id, got %x expected %x\n",
  800. ret, ISL29501_DEVICE_ID);
  801. return -ENODEV;
  802. }
  803. ret = isl29501_reset_registers(isl29501);
  804. if (ret < 0)
  805. return ret;
  806. return isl29501_begin_acquisition(isl29501);
  807. }
  808. static irqreturn_t isl29501_trigger_handler(int irq, void *p)
  809. {
  810. struct iio_poll_func *pf = p;
  811. struct iio_dev *indio_dev = pf->indio_dev;
  812. struct isl29501_private *isl29501 = iio_priv(indio_dev);
  813. const unsigned long *active_mask = indio_dev->active_scan_mask;
  814. u32 buffer[4] = {}; /* 1x16-bit + ts */
  815. if (test_bit(ISL29501_DISTANCE_SCAN_INDEX, active_mask))
  816. isl29501_register_read(isl29501, REG_DISTANCE, buffer);
  817. iio_push_to_buffers_with_timestamp(indio_dev, buffer, pf->timestamp);
  818. iio_trigger_notify_done(indio_dev->trig);
  819. return IRQ_HANDLED;
  820. }
  821. static int isl29501_probe(struct i2c_client *client,
  822. const struct i2c_device_id *id)
  823. {
  824. struct iio_dev *indio_dev;
  825. struct isl29501_private *isl29501;
  826. int ret;
  827. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*isl29501));
  828. if (!indio_dev)
  829. return -ENOMEM;
  830. isl29501 = iio_priv(indio_dev);
  831. i2c_set_clientdata(client, indio_dev);
  832. isl29501->client = client;
  833. mutex_init(&isl29501->lock);
  834. ret = isl29501_init_chip(isl29501);
  835. if (ret < 0)
  836. return ret;
  837. indio_dev->modes = INDIO_DIRECT_MODE;
  838. indio_dev->dev.parent = &client->dev;
  839. indio_dev->channels = isl29501_channels;
  840. indio_dev->num_channels = ARRAY_SIZE(isl29501_channels);
  841. indio_dev->name = client->name;
  842. indio_dev->info = &isl29501_info;
  843. ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
  844. iio_pollfunc_store_time,
  845. isl29501_trigger_handler,
  846. NULL);
  847. if (ret < 0) {
  848. dev_err(&client->dev, "unable to setup iio triggered buffer\n");
  849. return ret;
  850. }
  851. return devm_iio_device_register(&client->dev, indio_dev);
  852. }
  853. static const struct i2c_device_id isl29501_id[] = {
  854. {"isl29501", 0},
  855. {}
  856. };
  857. MODULE_DEVICE_TABLE(i2c, isl29501_id);
  858. #if defined(CONFIG_OF)
  859. static const struct of_device_id isl29501_i2c_matches[] = {
  860. { .compatible = "renesas,isl29501" },
  861. { }
  862. };
  863. MODULE_DEVICE_TABLE(of, isl29501_i2c_matches);
  864. #endif
  865. static struct i2c_driver isl29501_driver = {
  866. .driver = {
  867. .name = "isl29501",
  868. },
  869. .id_table = isl29501_id,
  870. .probe = isl29501_probe,
  871. };
  872. module_i2c_driver(isl29501_driver);
  873. MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>");
  874. MODULE_DESCRIPTION("ISL29501 Time of Flight sensor driver");
  875. MODULE_LICENSE("GPL v2");