si1145.c 35 KB

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