si1145.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * si1145.c - Support for Silabs SI1132 and SI1141/2/3/5/6/7 combined ambient
  4. * light, UV index and proximity sensors
  5. *
  6. * Copyright 2014-16 Peter Meerwald-Stadler <[email protected]>
  7. * Copyright 2016 Crestez Dan Leonard <[email protected]>
  8. *
  9. * SI1132 (7-bit I2C slave address 0x60)
  10. * SI1141/2/3 (7-bit I2C slave address 0x5a)
  11. * SI1145/6/6 (7-bit I2C slave address 0x60)
  12. */
  13. #include <linux/module.h>
  14. #include <linux/i2c.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/irq.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/trigger.h>
  22. #include <linux/iio/trigger_consumer.h>
  23. #include <linux/iio/triggered_buffer.h>
  24. #include <linux/iio/buffer.h>
  25. #include <linux/util_macros.h>
  26. #define SI1145_REG_PART_ID 0x00
  27. #define SI1145_REG_REV_ID 0x01
  28. #define SI1145_REG_SEQ_ID 0x02
  29. #define SI1145_REG_INT_CFG 0x03
  30. #define SI1145_REG_IRQ_ENABLE 0x04
  31. #define SI1145_REG_IRQ_MODE 0x05
  32. #define SI1145_REG_HW_KEY 0x07
  33. #define SI1145_REG_MEAS_RATE 0x08
  34. #define SI1145_REG_PS_LED21 0x0f
  35. #define SI1145_REG_PS_LED3 0x10
  36. #define SI1145_REG_UCOEF1 0x13
  37. #define SI1145_REG_UCOEF2 0x14
  38. #define SI1145_REG_UCOEF3 0x15
  39. #define SI1145_REG_UCOEF4 0x16
  40. #define SI1145_REG_PARAM_WR 0x17
  41. #define SI1145_REG_COMMAND 0x18
  42. #define SI1145_REG_RESPONSE 0x20
  43. #define SI1145_REG_IRQ_STATUS 0x21
  44. #define SI1145_REG_ALSVIS_DATA 0x22
  45. #define SI1145_REG_ALSIR_DATA 0x24
  46. #define SI1145_REG_PS1_DATA 0x26
  47. #define SI1145_REG_PS2_DATA 0x28
  48. #define SI1145_REG_PS3_DATA 0x2a
  49. #define SI1145_REG_AUX_DATA 0x2c
  50. #define SI1145_REG_PARAM_RD 0x2e
  51. #define SI1145_REG_CHIP_STAT 0x30
  52. #define SI1145_UCOEF1_DEFAULT 0x7b
  53. #define SI1145_UCOEF2_DEFAULT 0x6b
  54. #define SI1145_UCOEF3_DEFAULT 0x01
  55. #define SI1145_UCOEF4_DEFAULT 0x00
  56. /* Helper to figure out PS_LED register / shift per channel */
  57. #define SI1145_PS_LED_REG(ch) \
  58. (((ch) == 2) ? SI1145_REG_PS_LED3 : SI1145_REG_PS_LED21)
  59. #define SI1145_PS_LED_SHIFT(ch) \
  60. (((ch) == 1) ? 4 : 0)
  61. /* Parameter offsets */
  62. #define SI1145_PARAM_CHLIST 0x01
  63. #define SI1145_PARAM_PSLED12_SELECT 0x02
  64. #define SI1145_PARAM_PSLED3_SELECT 0x03
  65. #define SI1145_PARAM_PS_ENCODING 0x05
  66. #define SI1145_PARAM_ALS_ENCODING 0x06
  67. #define SI1145_PARAM_PS1_ADC_MUX 0x07
  68. #define SI1145_PARAM_PS2_ADC_MUX 0x08
  69. #define SI1145_PARAM_PS3_ADC_MUX 0x09
  70. #define SI1145_PARAM_PS_ADC_COUNTER 0x0a
  71. #define SI1145_PARAM_PS_ADC_GAIN 0x0b
  72. #define SI1145_PARAM_PS_ADC_MISC 0x0c
  73. #define SI1145_PARAM_ALS_ADC_MUX 0x0d
  74. #define SI1145_PARAM_ALSIR_ADC_MUX 0x0e
  75. #define SI1145_PARAM_AUX_ADC_MUX 0x0f
  76. #define SI1145_PARAM_ALSVIS_ADC_COUNTER 0x10
  77. #define SI1145_PARAM_ALSVIS_ADC_GAIN 0x11
  78. #define SI1145_PARAM_ALSVIS_ADC_MISC 0x12
  79. #define SI1145_PARAM_LED_RECOVERY 0x1c
  80. #define SI1145_PARAM_ALSIR_ADC_COUNTER 0x1d
  81. #define SI1145_PARAM_ALSIR_ADC_GAIN 0x1e
  82. #define SI1145_PARAM_ALSIR_ADC_MISC 0x1f
  83. #define SI1145_PARAM_ADC_OFFSET 0x1a
  84. /* Channel enable masks for CHLIST parameter */
  85. #define SI1145_CHLIST_EN_PS1 BIT(0)
  86. #define SI1145_CHLIST_EN_PS2 BIT(1)
  87. #define SI1145_CHLIST_EN_PS3 BIT(2)
  88. #define SI1145_CHLIST_EN_ALSVIS BIT(4)
  89. #define SI1145_CHLIST_EN_ALSIR BIT(5)
  90. #define SI1145_CHLIST_EN_AUX BIT(6)
  91. #define SI1145_CHLIST_EN_UV BIT(7)
  92. /* Proximity measurement mode for ADC_MISC parameter */
  93. #define SI1145_PS_ADC_MODE_NORMAL BIT(2)
  94. /* Signal range mask for ADC_MISC parameter */
  95. #define SI1145_ADC_MISC_RANGE BIT(5)
  96. /* Commands for REG_COMMAND */
  97. #define SI1145_CMD_NOP 0x00
  98. #define SI1145_CMD_RESET 0x01
  99. #define SI1145_CMD_PS_FORCE 0x05
  100. #define SI1145_CMD_ALS_FORCE 0x06
  101. #define SI1145_CMD_PSALS_FORCE 0x07
  102. #define SI1145_CMD_PS_PAUSE 0x09
  103. #define SI1145_CMD_ALS_PAUSE 0x0a
  104. #define SI1145_CMD_PSALS_PAUSE 0x0b
  105. #define SI1145_CMD_PS_AUTO 0x0d
  106. #define SI1145_CMD_ALS_AUTO 0x0e
  107. #define SI1145_CMD_PSALS_AUTO 0x0f
  108. #define SI1145_CMD_PARAM_QUERY 0x80
  109. #define SI1145_CMD_PARAM_SET 0xa0
  110. #define SI1145_RSP_INVALID_SETTING 0x80
  111. #define SI1145_RSP_COUNTER_MASK 0x0F
  112. /* Minimum sleep after each command to ensure it's received */
  113. #define SI1145_COMMAND_MINSLEEP_MS 5
  114. /* Return -ETIMEDOUT after this long */
  115. #define SI1145_COMMAND_TIMEOUT_MS 25
  116. /* Interrupt configuration masks for INT_CFG register */
  117. #define SI1145_INT_CFG_OE BIT(0) /* enable interrupt */
  118. #define SI1145_INT_CFG_MODE BIT(1) /* auto reset interrupt pin */
  119. /* Interrupt enable masks for IRQ_ENABLE register */
  120. #define SI1145_MASK_ALL_IE (BIT(4) | BIT(3) | BIT(2) | BIT(0))
  121. #define SI1145_MUX_TEMP 0x65
  122. #define SI1145_MUX_VDD 0x75
  123. /* Proximity LED current; see Table 2 in datasheet */
  124. #define SI1145_LED_CURRENT_45mA 0x04
  125. enum {
  126. SI1132,
  127. SI1141,
  128. SI1142,
  129. SI1143,
  130. SI1145,
  131. SI1146,
  132. SI1147,
  133. };
  134. struct si1145_part_info {
  135. u8 part;
  136. const struct iio_info *iio_info;
  137. const struct iio_chan_spec *channels;
  138. unsigned int num_channels;
  139. unsigned int num_leds;
  140. bool uncompressed_meas_rate;
  141. };
  142. /**
  143. * struct si1145_data - si1145 chip state data
  144. * @client: I2C client
  145. * @lock: mutex to protect shared state.
  146. * @cmdlock: Low-level mutex to protect command execution only
  147. * @rsp_seq: Next expected response number or -1 if counter reset required
  148. * @scan_mask: Saved scan mask to avoid duplicate set_chlist
  149. * @autonomous: If automatic measurements are active (for buffer support)
  150. * @part_info: Part information
  151. * @trig: Pointer to iio trigger
  152. * @meas_rate: Value of MEAS_RATE register. Only set in HW in auto mode
  153. * @buffer: Used to pack data read from sensor.
  154. */
  155. struct si1145_data {
  156. struct i2c_client *client;
  157. struct mutex lock;
  158. struct mutex cmdlock;
  159. int rsp_seq;
  160. const struct si1145_part_info *part_info;
  161. unsigned long scan_mask;
  162. bool autonomous;
  163. struct iio_trigger *trig;
  164. int meas_rate;
  165. /*
  166. * Ensure timestamp will be naturally aligned if present.
  167. * Maximum buffer size (may be only partly used if not all
  168. * channels are enabled):
  169. * 6*2 bytes channels data + 4 bytes alignment +
  170. * 8 bytes timestamp
  171. */
  172. u8 buffer[24] __aligned(8);
  173. };
  174. /*
  175. * __si1145_command_reset() - Send CMD_NOP and wait for response 0
  176. *
  177. * Does not modify data->rsp_seq
  178. *
  179. * Return: 0 on success and -errno on error.
  180. */
  181. static int __si1145_command_reset(struct si1145_data *data)
  182. {
  183. struct device *dev = &data->client->dev;
  184. unsigned long stop_jiffies;
  185. int ret;
  186. ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND,
  187. SI1145_CMD_NOP);
  188. if (ret < 0)
  189. return ret;
  190. msleep(SI1145_COMMAND_MINSLEEP_MS);
  191. stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
  192. while (true) {
  193. ret = i2c_smbus_read_byte_data(data->client,
  194. SI1145_REG_RESPONSE);
  195. if (ret <= 0)
  196. return ret;
  197. if (time_after(jiffies, stop_jiffies)) {
  198. dev_warn(dev, "timeout on reset\n");
  199. return -ETIMEDOUT;
  200. }
  201. msleep(SI1145_COMMAND_MINSLEEP_MS);
  202. }
  203. }
  204. /*
  205. * si1145_command() - Execute a command and poll the response register
  206. *
  207. * All conversion overflows are reported as -EOVERFLOW
  208. * INVALID_SETTING is reported as -EINVAL
  209. * Timeouts are reported as -ETIMEDOUT
  210. *
  211. * Return: 0 on success or -errno on failure
  212. */
  213. static int si1145_command(struct si1145_data *data, u8 cmd)
  214. {
  215. struct device *dev = &data->client->dev;
  216. unsigned long stop_jiffies;
  217. int ret;
  218. mutex_lock(&data->cmdlock);
  219. if (data->rsp_seq < 0) {
  220. ret = __si1145_command_reset(data);
  221. if (ret < 0) {
  222. dev_err(dev, "failed to reset command counter, ret=%d\n",
  223. ret);
  224. goto out;
  225. }
  226. data->rsp_seq = 0;
  227. }
  228. ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND, cmd);
  229. if (ret) {
  230. dev_warn(dev, "failed to write command, ret=%d\n", ret);
  231. goto out;
  232. }
  233. /* Sleep a little to ensure the command is received */
  234. msleep(SI1145_COMMAND_MINSLEEP_MS);
  235. stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
  236. while (true) {
  237. ret = i2c_smbus_read_byte_data(data->client,
  238. SI1145_REG_RESPONSE);
  239. if (ret < 0) {
  240. dev_warn(dev, "failed to read response, ret=%d\n", ret);
  241. break;
  242. }
  243. if ((ret & ~SI1145_RSP_COUNTER_MASK) == 0) {
  244. if (ret == data->rsp_seq) {
  245. if (time_after(jiffies, stop_jiffies)) {
  246. dev_warn(dev, "timeout on command 0x%02x\n",
  247. cmd);
  248. ret = -ETIMEDOUT;
  249. break;
  250. }
  251. msleep(SI1145_COMMAND_MINSLEEP_MS);
  252. continue;
  253. }
  254. if (ret == ((data->rsp_seq + 1) &
  255. SI1145_RSP_COUNTER_MASK)) {
  256. data->rsp_seq = ret;
  257. ret = 0;
  258. break;
  259. }
  260. dev_warn(dev, "unexpected response counter %d instead of %d\n",
  261. ret, (data->rsp_seq + 1) &
  262. SI1145_RSP_COUNTER_MASK);
  263. ret = -EIO;
  264. } else {
  265. if (ret == SI1145_RSP_INVALID_SETTING) {
  266. dev_warn(dev, "INVALID_SETTING error on command 0x%02x\n",
  267. cmd);
  268. ret = -EINVAL;
  269. } else {
  270. /* All overflows are treated identically */
  271. dev_dbg(dev, "overflow, ret=%d, cmd=0x%02x\n",
  272. ret, cmd);
  273. ret = -EOVERFLOW;
  274. }
  275. }
  276. /* Force a counter reset next time */
  277. data->rsp_seq = -1;
  278. break;
  279. }
  280. out:
  281. mutex_unlock(&data->cmdlock);
  282. return ret;
  283. }
  284. static int si1145_param_update(struct si1145_data *data, u8 op, u8 param,
  285. u8 value)
  286. {
  287. int ret;
  288. ret = i2c_smbus_write_byte_data(data->client,
  289. SI1145_REG_PARAM_WR, value);
  290. if (ret < 0)
  291. return ret;
  292. return si1145_command(data, op | (param & 0x1F));
  293. }
  294. static int si1145_param_set(struct si1145_data *data, u8 param, u8 value)
  295. {
  296. return si1145_param_update(data, SI1145_CMD_PARAM_SET, param, value);
  297. }
  298. /* Set param. Returns negative errno or current value */
  299. static int si1145_param_query(struct si1145_data *data, u8 param)
  300. {
  301. int ret;
  302. ret = si1145_command(data, SI1145_CMD_PARAM_QUERY | (param & 0x1F));
  303. if (ret < 0)
  304. return ret;
  305. return i2c_smbus_read_byte_data(data->client, SI1145_REG_PARAM_RD);
  306. }
  307. /* Expand 8 bit compressed value to 16 bit, see Silabs AN498 */
  308. static u16 si1145_uncompress(u8 x)
  309. {
  310. u16 result = 0;
  311. u8 exponent = 0;
  312. if (x < 8)
  313. return 0;
  314. exponent = (x & 0xf0) >> 4;
  315. result = 0x10 | (x & 0x0f);
  316. if (exponent >= 4)
  317. return result << (exponent - 4);
  318. return result >> (4 - exponent);
  319. }
  320. /* Compress 16 bit value to 8 bit, see Silabs AN498 */
  321. static u8 si1145_compress(u16 x)
  322. {
  323. u32 exponent = 0;
  324. u32 significand = 0;
  325. u32 tmp = x;
  326. if (x == 0x0000)
  327. return 0x00;
  328. if (x == 0x0001)
  329. return 0x08;
  330. while (1) {
  331. tmp >>= 1;
  332. exponent += 1;
  333. if (tmp == 1)
  334. break;
  335. }
  336. if (exponent < 5) {
  337. significand = x << (4 - exponent);
  338. return (exponent << 4) | (significand & 0xF);
  339. }
  340. significand = x >> (exponent - 5);
  341. if (significand & 1) {
  342. significand += 2;
  343. if (significand & 0x0040) {
  344. exponent += 1;
  345. significand >>= 1;
  346. }
  347. }
  348. return (exponent << 4) | ((significand >> 1) & 0xF);
  349. }
  350. /* Write meas_rate in hardware */
  351. static int si1145_set_meas_rate(struct si1145_data *data, int interval)
  352. {
  353. if (data->part_info->uncompressed_meas_rate)
  354. return i2c_smbus_write_word_data(data->client,
  355. SI1145_REG_MEAS_RATE, interval);
  356. else
  357. return i2c_smbus_write_byte_data(data->client,
  358. SI1145_REG_MEAS_RATE, interval);
  359. }
  360. static int si1145_read_samp_freq(struct si1145_data *data, int *val, int *val2)
  361. {
  362. *val = 32000;
  363. if (data->part_info->uncompressed_meas_rate)
  364. *val2 = data->meas_rate;
  365. else
  366. *val2 = si1145_uncompress(data->meas_rate);
  367. return IIO_VAL_FRACTIONAL;
  368. }
  369. /* Set the samp freq in driver private data */
  370. static int si1145_store_samp_freq(struct si1145_data *data, int val)
  371. {
  372. int ret = 0;
  373. int meas_rate;
  374. if (val <= 0 || val > 32000)
  375. return -ERANGE;
  376. meas_rate = 32000 / val;
  377. mutex_lock(&data->lock);
  378. if (data->autonomous) {
  379. ret = si1145_set_meas_rate(data, meas_rate);
  380. if (ret)
  381. goto out;
  382. }
  383. if (data->part_info->uncompressed_meas_rate)
  384. data->meas_rate = meas_rate;
  385. else
  386. data->meas_rate = si1145_compress(meas_rate);
  387. out:
  388. mutex_unlock(&data->lock);
  389. return ret;
  390. }
  391. static irqreturn_t si1145_trigger_handler(int irq, void *private)
  392. {
  393. struct iio_poll_func *pf = private;
  394. struct iio_dev *indio_dev = pf->indio_dev;
  395. struct si1145_data *data = iio_priv(indio_dev);
  396. int i, j = 0;
  397. int ret;
  398. u8 irq_status = 0;
  399. if (!data->autonomous) {
  400. ret = si1145_command(data, SI1145_CMD_PSALS_FORCE);
  401. if (ret < 0 && ret != -EOVERFLOW)
  402. goto done;
  403. } else {
  404. irq_status = ret = i2c_smbus_read_byte_data(data->client,
  405. SI1145_REG_IRQ_STATUS);
  406. if (ret < 0)
  407. goto done;
  408. if (!(irq_status & SI1145_MASK_ALL_IE))
  409. goto done;
  410. }
  411. for_each_set_bit(i, indio_dev->active_scan_mask,
  412. indio_dev->masklength) {
  413. int run = 1;
  414. while (i + run < indio_dev->masklength) {
  415. if (!test_bit(i + run, indio_dev->active_scan_mask))
  416. break;
  417. if (indio_dev->channels[i + run].address !=
  418. indio_dev->channels[i].address + 2 * run)
  419. break;
  420. run++;
  421. }
  422. ret = i2c_smbus_read_i2c_block_data_or_emulated(
  423. data->client, indio_dev->channels[i].address,
  424. sizeof(u16) * run, &data->buffer[j]);
  425. if (ret < 0)
  426. goto done;
  427. j += run * sizeof(u16);
  428. i += run - 1;
  429. }
  430. if (data->autonomous) {
  431. ret = i2c_smbus_write_byte_data(data->client,
  432. SI1145_REG_IRQ_STATUS,
  433. irq_status & SI1145_MASK_ALL_IE);
  434. if (ret < 0)
  435. goto done;
  436. }
  437. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  438. iio_get_time_ns(indio_dev));
  439. done:
  440. iio_trigger_notify_done(indio_dev->trig);
  441. return IRQ_HANDLED;
  442. }
  443. static int si1145_set_chlist(struct iio_dev *indio_dev, unsigned long scan_mask)
  444. {
  445. struct si1145_data *data = iio_priv(indio_dev);
  446. u8 reg = 0, mux;
  447. int ret;
  448. int i;
  449. /* channel list already set, no need to reprogram */
  450. if (data->scan_mask == scan_mask)
  451. return 0;
  452. for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
  453. switch (indio_dev->channels[i].address) {
  454. case SI1145_REG_ALSVIS_DATA:
  455. reg |= SI1145_CHLIST_EN_ALSVIS;
  456. break;
  457. case SI1145_REG_ALSIR_DATA:
  458. reg |= SI1145_CHLIST_EN_ALSIR;
  459. break;
  460. case SI1145_REG_PS1_DATA:
  461. reg |= SI1145_CHLIST_EN_PS1;
  462. break;
  463. case SI1145_REG_PS2_DATA:
  464. reg |= SI1145_CHLIST_EN_PS2;
  465. break;
  466. case SI1145_REG_PS3_DATA:
  467. reg |= SI1145_CHLIST_EN_PS3;
  468. break;
  469. case SI1145_REG_AUX_DATA:
  470. switch (indio_dev->channels[i].type) {
  471. case IIO_UVINDEX:
  472. reg |= SI1145_CHLIST_EN_UV;
  473. break;
  474. default:
  475. reg |= SI1145_CHLIST_EN_AUX;
  476. if (indio_dev->channels[i].type == IIO_TEMP)
  477. mux = SI1145_MUX_TEMP;
  478. else
  479. mux = SI1145_MUX_VDD;
  480. ret = si1145_param_set(data,
  481. SI1145_PARAM_AUX_ADC_MUX, mux);
  482. if (ret < 0)
  483. return ret;
  484. break;
  485. }
  486. }
  487. }
  488. data->scan_mask = scan_mask;
  489. ret = si1145_param_set(data, SI1145_PARAM_CHLIST, reg);
  490. return ret < 0 ? ret : 0;
  491. }
  492. static int si1145_measure(struct iio_dev *indio_dev,
  493. struct iio_chan_spec const *chan)
  494. {
  495. struct si1145_data *data = iio_priv(indio_dev);
  496. u8 cmd;
  497. int ret;
  498. ret = si1145_set_chlist(indio_dev, BIT(chan->scan_index));
  499. if (ret < 0)
  500. return ret;
  501. cmd = (chan->type == IIO_PROXIMITY) ? SI1145_CMD_PS_FORCE :
  502. SI1145_CMD_ALS_FORCE;
  503. ret = si1145_command(data, cmd);
  504. if (ret < 0 && ret != -EOVERFLOW)
  505. return ret;
  506. return i2c_smbus_read_word_data(data->client, chan->address);
  507. }
  508. /*
  509. * Conversion between iio scale and ADC_GAIN values
  510. * These could be further adjusted but proximity/intensity are dimensionless
  511. */
  512. static const int si1145_proximity_scale_available[] = {
  513. 128, 64, 32, 16, 8, 4};
  514. static const int si1145_intensity_scale_available[] = {
  515. 128, 64, 32, 16, 8, 4, 2, 1};
  516. static IIO_CONST_ATTR(in_proximity_scale_available,
  517. "128 64 32 16 8 4");
  518. static IIO_CONST_ATTR(in_intensity_scale_available,
  519. "128 64 32 16 8 4 2 1");
  520. static IIO_CONST_ATTR(in_intensity_ir_scale_available,
  521. "128 64 32 16 8 4 2 1");
  522. static int si1145_scale_from_adcgain(int regval)
  523. {
  524. return 128 >> regval;
  525. }
  526. static int si1145_proximity_adcgain_from_scale(int val, int val2)
  527. {
  528. val = find_closest_descending(val, si1145_proximity_scale_available,
  529. ARRAY_SIZE(si1145_proximity_scale_available));
  530. if (val < 0 || val > 5 || val2 != 0)
  531. return -EINVAL;
  532. return val;
  533. }
  534. static int si1145_intensity_adcgain_from_scale(int val, int val2)
  535. {
  536. val = find_closest_descending(val, si1145_intensity_scale_available,
  537. ARRAY_SIZE(si1145_intensity_scale_available));
  538. if (val < 0 || val > 7 || val2 != 0)
  539. return -EINVAL;
  540. return val;
  541. }
  542. static int si1145_read_raw(struct iio_dev *indio_dev,
  543. struct iio_chan_spec const *chan,
  544. int *val, int *val2, long mask)
  545. {
  546. struct si1145_data *data = iio_priv(indio_dev);
  547. int ret;
  548. u8 reg;
  549. switch (mask) {
  550. case IIO_CHAN_INFO_RAW:
  551. switch (chan->type) {
  552. case IIO_INTENSITY:
  553. case IIO_PROXIMITY:
  554. case IIO_VOLTAGE:
  555. case IIO_TEMP:
  556. case IIO_UVINDEX:
  557. ret = iio_device_claim_direct_mode(indio_dev);
  558. if (ret)
  559. return ret;
  560. ret = si1145_measure(indio_dev, chan);
  561. iio_device_release_direct_mode(indio_dev);
  562. if (ret < 0)
  563. return ret;
  564. *val = ret;
  565. return IIO_VAL_INT;
  566. case IIO_CURRENT:
  567. ret = i2c_smbus_read_byte_data(data->client,
  568. SI1145_PS_LED_REG(chan->channel));
  569. if (ret < 0)
  570. return ret;
  571. *val = (ret >> SI1145_PS_LED_SHIFT(chan->channel))
  572. & 0x0f;
  573. return IIO_VAL_INT;
  574. default:
  575. return -EINVAL;
  576. }
  577. case IIO_CHAN_INFO_SCALE:
  578. switch (chan->type) {
  579. case IIO_PROXIMITY:
  580. reg = SI1145_PARAM_PS_ADC_GAIN;
  581. break;
  582. case IIO_INTENSITY:
  583. if (chan->channel2 == IIO_MOD_LIGHT_IR)
  584. reg = SI1145_PARAM_ALSIR_ADC_GAIN;
  585. else
  586. reg = SI1145_PARAM_ALSVIS_ADC_GAIN;
  587. break;
  588. case IIO_TEMP:
  589. *val = 28;
  590. *val2 = 571429;
  591. return IIO_VAL_INT_PLUS_MICRO;
  592. case IIO_UVINDEX:
  593. *val = 0;
  594. *val2 = 10000;
  595. return IIO_VAL_INT_PLUS_MICRO;
  596. default:
  597. return -EINVAL;
  598. }
  599. ret = si1145_param_query(data, reg);
  600. if (ret < 0)
  601. return ret;
  602. *val = si1145_scale_from_adcgain(ret & 0x07);
  603. return IIO_VAL_INT;
  604. case IIO_CHAN_INFO_OFFSET:
  605. switch (chan->type) {
  606. case IIO_TEMP:
  607. /*
  608. * -ADC offset - ADC counts @ 25°C -
  609. * 35 * ADC counts / °C
  610. */
  611. *val = -256 - 11136 + 25 * 35;
  612. return IIO_VAL_INT;
  613. default:
  614. /*
  615. * All ADC measurements have are by default offset
  616. * by -256
  617. * See AN498 5.6.3
  618. */
  619. ret = si1145_param_query(data, SI1145_PARAM_ADC_OFFSET);
  620. if (ret < 0)
  621. return ret;
  622. *val = -si1145_uncompress(ret);
  623. return IIO_VAL_INT;
  624. }
  625. case IIO_CHAN_INFO_SAMP_FREQ:
  626. return si1145_read_samp_freq(data, val, val2);
  627. default:
  628. return -EINVAL;
  629. }
  630. }
  631. static int si1145_write_raw(struct iio_dev *indio_dev,
  632. struct iio_chan_spec const *chan,
  633. int val, int val2, long mask)
  634. {
  635. struct si1145_data *data = iio_priv(indio_dev);
  636. u8 reg1, reg2, shift;
  637. int ret;
  638. switch (mask) {
  639. case IIO_CHAN_INFO_SCALE:
  640. switch (chan->type) {
  641. case IIO_PROXIMITY:
  642. val = si1145_proximity_adcgain_from_scale(val, val2);
  643. if (val < 0)
  644. return val;
  645. reg1 = SI1145_PARAM_PS_ADC_GAIN;
  646. reg2 = SI1145_PARAM_PS_ADC_COUNTER;
  647. break;
  648. case IIO_INTENSITY:
  649. val = si1145_intensity_adcgain_from_scale(val, val2);
  650. if (val < 0)
  651. return val;
  652. if (chan->channel2 == IIO_MOD_LIGHT_IR) {
  653. reg1 = SI1145_PARAM_ALSIR_ADC_GAIN;
  654. reg2 = SI1145_PARAM_ALSIR_ADC_COUNTER;
  655. } else {
  656. reg1 = SI1145_PARAM_ALSVIS_ADC_GAIN;
  657. reg2 = SI1145_PARAM_ALSVIS_ADC_COUNTER;
  658. }
  659. break;
  660. default:
  661. return -EINVAL;
  662. }
  663. ret = iio_device_claim_direct_mode(indio_dev);
  664. if (ret)
  665. return ret;
  666. ret = si1145_param_set(data, reg1, val);
  667. if (ret < 0) {
  668. iio_device_release_direct_mode(indio_dev);
  669. return ret;
  670. }
  671. /* Set recovery period to one's complement of gain */
  672. ret = si1145_param_set(data, reg2, (~val & 0x07) << 4);
  673. iio_device_release_direct_mode(indio_dev);
  674. return ret;
  675. case IIO_CHAN_INFO_RAW:
  676. if (chan->type != IIO_CURRENT)
  677. return -EINVAL;
  678. if (val < 0 || val > 15 || val2 != 0)
  679. return -EINVAL;
  680. reg1 = SI1145_PS_LED_REG(chan->channel);
  681. shift = SI1145_PS_LED_SHIFT(chan->channel);
  682. ret = iio_device_claim_direct_mode(indio_dev);
  683. if (ret)
  684. return ret;
  685. ret = i2c_smbus_read_byte_data(data->client, reg1);
  686. if (ret < 0) {
  687. iio_device_release_direct_mode(indio_dev);
  688. return ret;
  689. }
  690. ret = i2c_smbus_write_byte_data(data->client, reg1,
  691. (ret & ~(0x0f << shift)) |
  692. ((val & 0x0f) << shift));
  693. iio_device_release_direct_mode(indio_dev);
  694. return ret;
  695. case IIO_CHAN_INFO_SAMP_FREQ:
  696. return si1145_store_samp_freq(data, val);
  697. default:
  698. return -EINVAL;
  699. }
  700. }
  701. #define SI1145_ST { \
  702. .sign = 'u', \
  703. .realbits = 16, \
  704. .storagebits = 16, \
  705. .endianness = IIO_LE, \
  706. }
  707. #define SI1145_INTENSITY_CHANNEL(_si) { \
  708. .type = IIO_INTENSITY, \
  709. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  710. BIT(IIO_CHAN_INFO_OFFSET) | \
  711. BIT(IIO_CHAN_INFO_SCALE), \
  712. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  713. .scan_type = SI1145_ST, \
  714. .scan_index = _si, \
  715. .address = SI1145_REG_ALSVIS_DATA, \
  716. }
  717. #define SI1145_INTENSITY_IR_CHANNEL(_si) { \
  718. .type = IIO_INTENSITY, \
  719. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  720. BIT(IIO_CHAN_INFO_OFFSET) | \
  721. BIT(IIO_CHAN_INFO_SCALE), \
  722. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  723. .modified = 1, \
  724. .channel2 = IIO_MOD_LIGHT_IR, \
  725. .scan_type = SI1145_ST, \
  726. .scan_index = _si, \
  727. .address = SI1145_REG_ALSIR_DATA, \
  728. }
  729. #define SI1145_TEMP_CHANNEL(_si) { \
  730. .type = IIO_TEMP, \
  731. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  732. BIT(IIO_CHAN_INFO_OFFSET) | \
  733. BIT(IIO_CHAN_INFO_SCALE), \
  734. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  735. .scan_type = SI1145_ST, \
  736. .scan_index = _si, \
  737. .address = SI1145_REG_AUX_DATA, \
  738. }
  739. #define SI1145_UV_CHANNEL(_si) { \
  740. .type = IIO_UVINDEX, \
  741. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  742. BIT(IIO_CHAN_INFO_SCALE), \
  743. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  744. .scan_type = SI1145_ST, \
  745. .scan_index = _si, \
  746. .address = SI1145_REG_AUX_DATA, \
  747. }
  748. #define SI1145_PROXIMITY_CHANNEL(_si, _ch) { \
  749. .type = IIO_PROXIMITY, \
  750. .indexed = 1, \
  751. .channel = _ch, \
  752. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  753. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  754. BIT(IIO_CHAN_INFO_OFFSET), \
  755. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  756. .scan_type = SI1145_ST, \
  757. .scan_index = _si, \
  758. .address = SI1145_REG_PS1_DATA + _ch * 2, \
  759. }
  760. #define SI1145_VOLTAGE_CHANNEL(_si) { \
  761. .type = IIO_VOLTAGE, \
  762. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  763. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  764. .scan_type = SI1145_ST, \
  765. .scan_index = _si, \
  766. .address = SI1145_REG_AUX_DATA, \
  767. }
  768. #define SI1145_CURRENT_CHANNEL(_ch) { \
  769. .type = IIO_CURRENT, \
  770. .indexed = 1, \
  771. .channel = _ch, \
  772. .output = 1, \
  773. .scan_index = -1, \
  774. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  775. }
  776. static const struct iio_chan_spec si1132_channels[] = {
  777. SI1145_INTENSITY_CHANNEL(0),
  778. SI1145_INTENSITY_IR_CHANNEL(1),
  779. SI1145_TEMP_CHANNEL(2),
  780. SI1145_VOLTAGE_CHANNEL(3),
  781. SI1145_UV_CHANNEL(4),
  782. IIO_CHAN_SOFT_TIMESTAMP(6),
  783. };
  784. static const struct iio_chan_spec si1141_channels[] = {
  785. SI1145_INTENSITY_CHANNEL(0),
  786. SI1145_INTENSITY_IR_CHANNEL(1),
  787. SI1145_PROXIMITY_CHANNEL(2, 0),
  788. SI1145_TEMP_CHANNEL(3),
  789. SI1145_VOLTAGE_CHANNEL(4),
  790. IIO_CHAN_SOFT_TIMESTAMP(5),
  791. SI1145_CURRENT_CHANNEL(0),
  792. };
  793. static const struct iio_chan_spec si1142_channels[] = {
  794. SI1145_INTENSITY_CHANNEL(0),
  795. SI1145_INTENSITY_IR_CHANNEL(1),
  796. SI1145_PROXIMITY_CHANNEL(2, 0),
  797. SI1145_PROXIMITY_CHANNEL(3, 1),
  798. SI1145_TEMP_CHANNEL(4),
  799. SI1145_VOLTAGE_CHANNEL(5),
  800. IIO_CHAN_SOFT_TIMESTAMP(6),
  801. SI1145_CURRENT_CHANNEL(0),
  802. SI1145_CURRENT_CHANNEL(1),
  803. };
  804. static const struct iio_chan_spec si1143_channels[] = {
  805. SI1145_INTENSITY_CHANNEL(0),
  806. SI1145_INTENSITY_IR_CHANNEL(1),
  807. SI1145_PROXIMITY_CHANNEL(2, 0),
  808. SI1145_PROXIMITY_CHANNEL(3, 1),
  809. SI1145_PROXIMITY_CHANNEL(4, 2),
  810. SI1145_TEMP_CHANNEL(5),
  811. SI1145_VOLTAGE_CHANNEL(6),
  812. IIO_CHAN_SOFT_TIMESTAMP(7),
  813. SI1145_CURRENT_CHANNEL(0),
  814. SI1145_CURRENT_CHANNEL(1),
  815. SI1145_CURRENT_CHANNEL(2),
  816. };
  817. static const struct iio_chan_spec si1145_channels[] = {
  818. SI1145_INTENSITY_CHANNEL(0),
  819. SI1145_INTENSITY_IR_CHANNEL(1),
  820. SI1145_PROXIMITY_CHANNEL(2, 0),
  821. SI1145_TEMP_CHANNEL(3),
  822. SI1145_VOLTAGE_CHANNEL(4),
  823. SI1145_UV_CHANNEL(5),
  824. IIO_CHAN_SOFT_TIMESTAMP(6),
  825. SI1145_CURRENT_CHANNEL(0),
  826. };
  827. static const struct iio_chan_spec si1146_channels[] = {
  828. SI1145_INTENSITY_CHANNEL(0),
  829. SI1145_INTENSITY_IR_CHANNEL(1),
  830. SI1145_TEMP_CHANNEL(2),
  831. SI1145_VOLTAGE_CHANNEL(3),
  832. SI1145_UV_CHANNEL(4),
  833. SI1145_PROXIMITY_CHANNEL(5, 0),
  834. SI1145_PROXIMITY_CHANNEL(6, 1),
  835. IIO_CHAN_SOFT_TIMESTAMP(7),
  836. SI1145_CURRENT_CHANNEL(0),
  837. SI1145_CURRENT_CHANNEL(1),
  838. };
  839. static const struct iio_chan_spec si1147_channels[] = {
  840. SI1145_INTENSITY_CHANNEL(0),
  841. SI1145_INTENSITY_IR_CHANNEL(1),
  842. SI1145_PROXIMITY_CHANNEL(2, 0),
  843. SI1145_PROXIMITY_CHANNEL(3, 1),
  844. SI1145_PROXIMITY_CHANNEL(4, 2),
  845. SI1145_TEMP_CHANNEL(5),
  846. SI1145_VOLTAGE_CHANNEL(6),
  847. SI1145_UV_CHANNEL(7),
  848. IIO_CHAN_SOFT_TIMESTAMP(8),
  849. SI1145_CURRENT_CHANNEL(0),
  850. SI1145_CURRENT_CHANNEL(1),
  851. SI1145_CURRENT_CHANNEL(2),
  852. };
  853. static struct attribute *si1132_attributes[] = {
  854. &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
  855. &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
  856. NULL,
  857. };
  858. static struct attribute *si114x_attributes[] = {
  859. &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
  860. &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
  861. &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
  862. NULL,
  863. };
  864. static const struct attribute_group si1132_attribute_group = {
  865. .attrs = si1132_attributes,
  866. };
  867. static const struct attribute_group si114x_attribute_group = {
  868. .attrs = si114x_attributes,
  869. };
  870. static const struct iio_info si1132_info = {
  871. .read_raw = si1145_read_raw,
  872. .write_raw = si1145_write_raw,
  873. .attrs = &si1132_attribute_group,
  874. };
  875. static const struct iio_info si114x_info = {
  876. .read_raw = si1145_read_raw,
  877. .write_raw = si1145_write_raw,
  878. .attrs = &si114x_attribute_group,
  879. };
  880. #define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \
  881. {id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate}
  882. static const struct si1145_part_info si1145_part_info[] = {
  883. [SI1132] = SI1145_PART(0x32, &si1132_info, si1132_channels, 0, true),
  884. [SI1141] = SI1145_PART(0x41, &si114x_info, si1141_channels, 1, false),
  885. [SI1142] = SI1145_PART(0x42, &si114x_info, si1142_channels, 2, false),
  886. [SI1143] = SI1145_PART(0x43, &si114x_info, si1143_channels, 3, false),
  887. [SI1145] = SI1145_PART(0x45, &si114x_info, si1145_channels, 1, true),
  888. [SI1146] = SI1145_PART(0x46, &si114x_info, si1146_channels, 2, true),
  889. [SI1147] = SI1145_PART(0x47, &si114x_info, si1147_channels, 3, true),
  890. };
  891. static int si1145_initialize(struct si1145_data *data)
  892. {
  893. struct i2c_client *client = data->client;
  894. int ret;
  895. ret = i2c_smbus_write_byte_data(client, SI1145_REG_COMMAND,
  896. SI1145_CMD_RESET);
  897. if (ret < 0)
  898. return ret;
  899. msleep(SI1145_COMMAND_TIMEOUT_MS);
  900. /* Hardware key, magic value */
  901. ret = i2c_smbus_write_byte_data(client, SI1145_REG_HW_KEY, 0x17);
  902. if (ret < 0)
  903. return ret;
  904. msleep(SI1145_COMMAND_TIMEOUT_MS);
  905. /* Turn off autonomous mode */
  906. ret = si1145_set_meas_rate(data, 0);
  907. if (ret < 0)
  908. return ret;
  909. /* Initialize sampling freq to 10 Hz */
  910. ret = si1145_store_samp_freq(data, 10);
  911. if (ret < 0)
  912. return ret;
  913. /* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */
  914. switch (data->part_info->num_leds) {
  915. case 3:
  916. ret = i2c_smbus_write_byte_data(client,
  917. SI1145_REG_PS_LED3,
  918. SI1145_LED_CURRENT_45mA);
  919. if (ret < 0)
  920. return ret;
  921. fallthrough;
  922. case 2:
  923. ret = i2c_smbus_write_byte_data(client,
  924. SI1145_REG_PS_LED21,
  925. (SI1145_LED_CURRENT_45mA << 4) |
  926. SI1145_LED_CURRENT_45mA);
  927. break;
  928. case 1:
  929. ret = i2c_smbus_write_byte_data(client,
  930. SI1145_REG_PS_LED21,
  931. SI1145_LED_CURRENT_45mA);
  932. break;
  933. default:
  934. ret = 0;
  935. break;
  936. }
  937. if (ret < 0)
  938. return ret;
  939. /* Set normal proximity measurement mode */
  940. ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_MISC,
  941. SI1145_PS_ADC_MODE_NORMAL);
  942. if (ret < 0)
  943. return ret;
  944. ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01);
  945. if (ret < 0)
  946. return ret;
  947. /* ADC_COUNTER should be one complement of ADC_GAIN */
  948. ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_COUNTER, 0x06 << 4);
  949. if (ret < 0)
  950. return ret;
  951. /* Set ALS visible measurement mode */
  952. ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_MISC,
  953. SI1145_ADC_MISC_RANGE);
  954. if (ret < 0)
  955. return ret;
  956. ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03);
  957. if (ret < 0)
  958. return ret;
  959. ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER,
  960. 0x04 << 4);
  961. if (ret < 0)
  962. return ret;
  963. /* Set ALS IR measurement mode */
  964. ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_MISC,
  965. SI1145_ADC_MISC_RANGE);
  966. if (ret < 0)
  967. return ret;
  968. ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01);
  969. if (ret < 0)
  970. return ret;
  971. ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER,
  972. 0x06 << 4);
  973. if (ret < 0)
  974. return ret;
  975. /*
  976. * Initialize UCOEF to default values in datasheet
  977. * These registers are normally zero on reset
  978. */
  979. if (data->part_info == &si1145_part_info[SI1132] ||
  980. data->part_info == &si1145_part_info[SI1145] ||
  981. data->part_info == &si1145_part_info[SI1146] ||
  982. data->part_info == &si1145_part_info[SI1147]) {
  983. ret = i2c_smbus_write_byte_data(data->client,
  984. SI1145_REG_UCOEF1,
  985. SI1145_UCOEF1_DEFAULT);
  986. if (ret < 0)
  987. return ret;
  988. ret = i2c_smbus_write_byte_data(data->client,
  989. SI1145_REG_UCOEF2, SI1145_UCOEF2_DEFAULT);
  990. if (ret < 0)
  991. return ret;
  992. ret = i2c_smbus_write_byte_data(data->client,
  993. SI1145_REG_UCOEF3, SI1145_UCOEF3_DEFAULT);
  994. if (ret < 0)
  995. return ret;
  996. ret = i2c_smbus_write_byte_data(data->client,
  997. SI1145_REG_UCOEF4, SI1145_UCOEF4_DEFAULT);
  998. if (ret < 0)
  999. return ret;
  1000. }
  1001. return 0;
  1002. }
  1003. /*
  1004. * Program the channels we want to measure with CMD_PSALS_AUTO. No need for
  1005. * _postdisable as we stop with CMD_PSALS_PAUSE; single measurement (direct)
  1006. * mode reprograms the channels list anyway...
  1007. */
  1008. static int si1145_buffer_preenable(struct iio_dev *indio_dev)
  1009. {
  1010. struct si1145_data *data = iio_priv(indio_dev);
  1011. int ret;
  1012. mutex_lock(&data->lock);
  1013. ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask);
  1014. mutex_unlock(&data->lock);
  1015. return ret;
  1016. }
  1017. static bool si1145_validate_scan_mask(struct iio_dev *indio_dev,
  1018. const unsigned long *scan_mask)
  1019. {
  1020. struct si1145_data *data = iio_priv(indio_dev);
  1021. unsigned int count = 0;
  1022. int i;
  1023. /* Check that at most one AUX channel is enabled */
  1024. for_each_set_bit(i, scan_mask, data->part_info->num_channels) {
  1025. if (indio_dev->channels[i].address == SI1145_REG_AUX_DATA)
  1026. count++;
  1027. }
  1028. return count <= 1;
  1029. }
  1030. static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = {
  1031. .preenable = si1145_buffer_preenable,
  1032. .validate_scan_mask = si1145_validate_scan_mask,
  1033. };
  1034. /*
  1035. * si1145_trigger_set_state() - Set trigger state
  1036. *
  1037. * When not using triggers interrupts are disabled and measurement rate is
  1038. * set to zero in order to minimize power consumption.
  1039. */
  1040. static int si1145_trigger_set_state(struct iio_trigger *trig, bool state)
  1041. {
  1042. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  1043. struct si1145_data *data = iio_priv(indio_dev);
  1044. int err = 0, ret;
  1045. mutex_lock(&data->lock);
  1046. if (state) {
  1047. data->autonomous = true;
  1048. err = i2c_smbus_write_byte_data(data->client,
  1049. SI1145_REG_INT_CFG, SI1145_INT_CFG_OE);
  1050. if (err < 0)
  1051. goto disable;
  1052. err = i2c_smbus_write_byte_data(data->client,
  1053. SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE);
  1054. if (err < 0)
  1055. goto disable;
  1056. err = si1145_set_meas_rate(data, data->meas_rate);
  1057. if (err < 0)
  1058. goto disable;
  1059. err = si1145_command(data, SI1145_CMD_PSALS_AUTO);
  1060. if (err < 0)
  1061. goto disable;
  1062. } else {
  1063. disable:
  1064. /* Disable as much as possible skipping errors */
  1065. ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE);
  1066. if (ret < 0 && !err)
  1067. err = ret;
  1068. ret = si1145_set_meas_rate(data, 0);
  1069. if (ret < 0 && !err)
  1070. err = ret;
  1071. ret = i2c_smbus_write_byte_data(data->client,
  1072. SI1145_REG_IRQ_ENABLE, 0);
  1073. if (ret < 0 && !err)
  1074. err = ret;
  1075. ret = i2c_smbus_write_byte_data(data->client,
  1076. SI1145_REG_INT_CFG, 0);
  1077. if (ret < 0 && !err)
  1078. err = ret;
  1079. data->autonomous = false;
  1080. }
  1081. mutex_unlock(&data->lock);
  1082. return err;
  1083. }
  1084. static const struct iio_trigger_ops si1145_trigger_ops = {
  1085. .set_trigger_state = si1145_trigger_set_state,
  1086. };
  1087. static int si1145_probe_trigger(struct iio_dev *indio_dev)
  1088. {
  1089. struct si1145_data *data = iio_priv(indio_dev);
  1090. struct i2c_client *client = data->client;
  1091. struct iio_trigger *trig;
  1092. int ret;
  1093. trig = devm_iio_trigger_alloc(&client->dev,
  1094. "%s-dev%d", indio_dev->name, iio_device_id(indio_dev));
  1095. if (!trig)
  1096. return -ENOMEM;
  1097. trig->ops = &si1145_trigger_ops;
  1098. iio_trigger_set_drvdata(trig, indio_dev);
  1099. ret = devm_request_irq(&client->dev, client->irq,
  1100. iio_trigger_generic_data_rdy_poll,
  1101. IRQF_TRIGGER_FALLING,
  1102. "si1145_irq",
  1103. trig);
  1104. if (ret < 0) {
  1105. dev_err(&client->dev, "irq request failed\n");
  1106. return ret;
  1107. }
  1108. ret = devm_iio_trigger_register(&client->dev, trig);
  1109. if (ret)
  1110. return ret;
  1111. data->trig = trig;
  1112. indio_dev->trig = iio_trigger_get(data->trig);
  1113. return 0;
  1114. }
  1115. static int si1145_probe(struct i2c_client *client,
  1116. const struct i2c_device_id *id)
  1117. {
  1118. struct si1145_data *data;
  1119. struct iio_dev *indio_dev;
  1120. u8 part_id, rev_id, seq_id;
  1121. int ret;
  1122. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  1123. if (!indio_dev)
  1124. return -ENOMEM;
  1125. data = iio_priv(indio_dev);
  1126. i2c_set_clientdata(client, indio_dev);
  1127. data->client = client;
  1128. data->part_info = &si1145_part_info[id->driver_data];
  1129. part_id = ret = i2c_smbus_read_byte_data(data->client,
  1130. SI1145_REG_PART_ID);
  1131. if (ret < 0)
  1132. return ret;
  1133. rev_id = ret = i2c_smbus_read_byte_data(data->client,
  1134. SI1145_REG_REV_ID);
  1135. if (ret < 0)
  1136. return ret;
  1137. seq_id = ret = i2c_smbus_read_byte_data(data->client,
  1138. SI1145_REG_SEQ_ID);
  1139. if (ret < 0)
  1140. return ret;
  1141. dev_info(&client->dev, "device ID part 0x%02x rev 0x%02x seq 0x%02x\n",
  1142. part_id, rev_id, seq_id);
  1143. if (part_id != data->part_info->part) {
  1144. dev_err(&client->dev, "part ID mismatch got 0x%02x, expected 0x%02x\n",
  1145. part_id, data->part_info->part);
  1146. return -ENODEV;
  1147. }
  1148. indio_dev->name = id->name;
  1149. indio_dev->channels = data->part_info->channels;
  1150. indio_dev->num_channels = data->part_info->num_channels;
  1151. indio_dev->info = data->part_info->iio_info;
  1152. indio_dev->modes = INDIO_DIRECT_MODE;
  1153. mutex_init(&data->lock);
  1154. mutex_init(&data->cmdlock);
  1155. ret = si1145_initialize(data);
  1156. if (ret < 0)
  1157. return ret;
  1158. ret = devm_iio_triggered_buffer_setup(&client->dev,
  1159. indio_dev, NULL,
  1160. si1145_trigger_handler, &si1145_buffer_setup_ops);
  1161. if (ret < 0)
  1162. return ret;
  1163. if (client->irq) {
  1164. ret = si1145_probe_trigger(indio_dev);
  1165. if (ret < 0)
  1166. return ret;
  1167. } else {
  1168. dev_info(&client->dev, "no irq, using polling\n");
  1169. }
  1170. return devm_iio_device_register(&client->dev, indio_dev);
  1171. }
  1172. static const struct i2c_device_id si1145_ids[] = {
  1173. { "si1132", SI1132 },
  1174. { "si1141", SI1141 },
  1175. { "si1142", SI1142 },
  1176. { "si1143", SI1143 },
  1177. { "si1145", SI1145 },
  1178. { "si1146", SI1146 },
  1179. { "si1147", SI1147 },
  1180. { }
  1181. };
  1182. MODULE_DEVICE_TABLE(i2c, si1145_ids);
  1183. static struct i2c_driver si1145_driver = {
  1184. .driver = {
  1185. .name = "si1145",
  1186. },
  1187. .probe = si1145_probe,
  1188. .id_table = si1145_ids,
  1189. };
  1190. module_i2c_driver(si1145_driver);
  1191. MODULE_AUTHOR("Peter Meerwald-Stadler <[email protected]>");
  1192. MODULE_DESCRIPTION("Silabs SI1132 and SI1141/2/3/5/6/7 proximity, ambient light and UV index sensor driver");
  1193. MODULE_LICENSE("GPL");