si1145.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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. .driver_module = THIS_MODULE,
  876. .attrs = &si1132_attribute_group,
  877. };
  878. static const struct iio_info si114x_info = {
  879. .read_raw = si1145_read_raw,
  880. .write_raw = si1145_write_raw,
  881. .driver_module = THIS_MODULE,
  882. .attrs = &si114x_attribute_group,
  883. };
  884. #define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \
  885. {id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate}
  886. static const struct si1145_part_info si1145_part_info[] = {
  887. [SI1132] = SI1145_PART(0x32, &si1132_info, si1132_channels, 0, true),
  888. [SI1141] = SI1145_PART(0x41, &si114x_info, si1141_channels, 1, false),
  889. [SI1142] = SI1145_PART(0x42, &si114x_info, si1142_channels, 2, false),
  890. [SI1143] = SI1145_PART(0x43, &si114x_info, si1143_channels, 3, false),
  891. [SI1145] = SI1145_PART(0x45, &si114x_info, si1145_channels, 1, true),
  892. [SI1146] = SI1145_PART(0x46, &si114x_info, si1146_channels, 2, true),
  893. [SI1147] = SI1145_PART(0x47, &si114x_info, si1147_channels, 3, true),
  894. };
  895. static int si1145_initialize(struct si1145_data *data)
  896. {
  897. struct i2c_client *client = data->client;
  898. int ret;
  899. ret = i2c_smbus_write_byte_data(client, SI1145_REG_COMMAND,
  900. SI1145_CMD_RESET);
  901. if (ret < 0)
  902. return ret;
  903. msleep(SI1145_COMMAND_TIMEOUT_MS);
  904. /* Hardware key, magic value */
  905. ret = i2c_smbus_write_byte_data(client, SI1145_REG_HW_KEY, 0x17);
  906. if (ret < 0)
  907. return ret;
  908. msleep(SI1145_COMMAND_TIMEOUT_MS);
  909. /* Turn off autonomous mode */
  910. ret = si1145_set_meas_rate(data, 0);
  911. if (ret < 0)
  912. return ret;
  913. /* Initialize sampling freq to 10 Hz */
  914. ret = si1145_store_samp_freq(data, 10);
  915. if (ret < 0)
  916. return ret;
  917. /* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */
  918. switch (data->part_info->num_leds) {
  919. case 3:
  920. ret = i2c_smbus_write_byte_data(client,
  921. SI1145_REG_PS_LED3,
  922. SI1145_LED_CURRENT_45mA);
  923. if (ret < 0)
  924. return ret;
  925. /* fallthrough */
  926. case 2:
  927. ret = i2c_smbus_write_byte_data(client,
  928. SI1145_REG_PS_LED21,
  929. (SI1145_LED_CURRENT_45mA << 4) |
  930. SI1145_LED_CURRENT_45mA);
  931. break;
  932. case 1:
  933. ret = i2c_smbus_write_byte_data(client,
  934. SI1145_REG_PS_LED21,
  935. SI1145_LED_CURRENT_45mA);
  936. break;
  937. default:
  938. ret = 0;
  939. break;
  940. }
  941. if (ret < 0)
  942. return ret;
  943. /* Set normal proximity measurement mode */
  944. ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_MISC,
  945. SI1145_PS_ADC_MODE_NORMAL);
  946. if (ret < 0)
  947. return ret;
  948. ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01);
  949. if (ret < 0)
  950. return ret;
  951. /* ADC_COUNTER should be one complement of ADC_GAIN */
  952. ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_COUNTER, 0x06 << 4);
  953. if (ret < 0)
  954. return ret;
  955. /* Set ALS visible measurement mode */
  956. ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_MISC,
  957. SI1145_ADC_MISC_RANGE);
  958. if (ret < 0)
  959. return ret;
  960. ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03);
  961. if (ret < 0)
  962. return ret;
  963. ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER,
  964. 0x04 << 4);
  965. if (ret < 0)
  966. return ret;
  967. /* Set ALS IR measurement mode */
  968. ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_MISC,
  969. SI1145_ADC_MISC_RANGE);
  970. if (ret < 0)
  971. return ret;
  972. ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01);
  973. if (ret < 0)
  974. return ret;
  975. ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER,
  976. 0x06 << 4);
  977. if (ret < 0)
  978. return ret;
  979. /*
  980. * Initialize UCOEF to default values in datasheet
  981. * These registers are normally zero on reset
  982. */
  983. if (data->part_info == &si1145_part_info[SI1132] ||
  984. data->part_info == &si1145_part_info[SI1145] ||
  985. data->part_info == &si1145_part_info[SI1146] ||
  986. data->part_info == &si1145_part_info[SI1147]) {
  987. ret = i2c_smbus_write_byte_data(data->client,
  988. SI1145_REG_UCOEF1,
  989. SI1145_UCOEF1_DEFAULT);
  990. if (ret < 0)
  991. return ret;
  992. ret = i2c_smbus_write_byte_data(data->client,
  993. SI1145_REG_UCOEF2, SI1145_UCOEF2_DEFAULT);
  994. if (ret < 0)
  995. return ret;
  996. ret = i2c_smbus_write_byte_data(data->client,
  997. SI1145_REG_UCOEF3, SI1145_UCOEF3_DEFAULT);
  998. if (ret < 0)
  999. return ret;
  1000. ret = i2c_smbus_write_byte_data(data->client,
  1001. SI1145_REG_UCOEF4, SI1145_UCOEF4_DEFAULT);
  1002. if (ret < 0)
  1003. return ret;
  1004. }
  1005. return 0;
  1006. }
  1007. /*
  1008. * Program the channels we want to measure with CMD_PSALS_AUTO. No need for
  1009. * _postdisable as we stop with CMD_PSALS_PAUSE; single measurement (direct)
  1010. * mode reprograms the channels list anyway...
  1011. */
  1012. static int si1145_buffer_preenable(struct iio_dev *indio_dev)
  1013. {
  1014. struct si1145_data *data = iio_priv(indio_dev);
  1015. int ret;
  1016. mutex_lock(&data->lock);
  1017. ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask);
  1018. mutex_unlock(&data->lock);
  1019. return ret;
  1020. }
  1021. static bool si1145_validate_scan_mask(struct iio_dev *indio_dev,
  1022. const unsigned long *scan_mask)
  1023. {
  1024. struct si1145_data *data = iio_priv(indio_dev);
  1025. unsigned int count = 0;
  1026. int i;
  1027. /* Check that at most one AUX channel is enabled */
  1028. for_each_set_bit(i, scan_mask, data->part_info->num_channels) {
  1029. if (indio_dev->channels[i].address == SI1145_REG_AUX_DATA)
  1030. count++;
  1031. }
  1032. return count <= 1;
  1033. }
  1034. static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = {
  1035. .preenable = si1145_buffer_preenable,
  1036. .postenable = iio_triggered_buffer_postenable,
  1037. .predisable = iio_triggered_buffer_predisable,
  1038. .validate_scan_mask = si1145_validate_scan_mask,
  1039. };
  1040. /**
  1041. * si1145_trigger_set_state() - Set trigger state
  1042. *
  1043. * When not using triggers interrupts are disabled and measurement rate is
  1044. * set to zero in order to minimize power consumption.
  1045. */
  1046. static int si1145_trigger_set_state(struct iio_trigger *trig, bool state)
  1047. {
  1048. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  1049. struct si1145_data *data = iio_priv(indio_dev);
  1050. int err = 0, ret;
  1051. mutex_lock(&data->lock);
  1052. if (state) {
  1053. data->autonomous = true;
  1054. err = i2c_smbus_write_byte_data(data->client,
  1055. SI1145_REG_INT_CFG, SI1145_INT_CFG_OE);
  1056. if (err < 0)
  1057. goto disable;
  1058. err = i2c_smbus_write_byte_data(data->client,
  1059. SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE);
  1060. if (err < 0)
  1061. goto disable;
  1062. err = si1145_set_meas_rate(data, data->meas_rate);
  1063. if (err < 0)
  1064. goto disable;
  1065. err = si1145_command(data, SI1145_CMD_PSALS_AUTO);
  1066. if (err < 0)
  1067. goto disable;
  1068. } else {
  1069. disable:
  1070. /* Disable as much as possible skipping errors */
  1071. ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE);
  1072. if (ret < 0 && !err)
  1073. err = ret;
  1074. ret = si1145_set_meas_rate(data, 0);
  1075. if (ret < 0 && !err)
  1076. err = ret;
  1077. ret = i2c_smbus_write_byte_data(data->client,
  1078. SI1145_REG_IRQ_ENABLE, 0);
  1079. if (ret < 0 && !err)
  1080. err = ret;
  1081. ret = i2c_smbus_write_byte_data(data->client,
  1082. SI1145_REG_INT_CFG, 0);
  1083. if (ret < 0 && !err)
  1084. err = ret;
  1085. data->autonomous = false;
  1086. }
  1087. mutex_unlock(&data->lock);
  1088. return err;
  1089. }
  1090. static const struct iio_trigger_ops si1145_trigger_ops = {
  1091. .owner = THIS_MODULE,
  1092. .set_trigger_state = si1145_trigger_set_state,
  1093. };
  1094. static int si1145_probe_trigger(struct iio_dev *indio_dev)
  1095. {
  1096. struct si1145_data *data = iio_priv(indio_dev);
  1097. struct i2c_client *client = data->client;
  1098. struct iio_trigger *trig;
  1099. int ret;
  1100. trig = devm_iio_trigger_alloc(&client->dev,
  1101. "%s-dev%d", indio_dev->name, indio_dev->id);
  1102. if (!trig)
  1103. return -ENOMEM;
  1104. trig->dev.parent = &client->dev;
  1105. trig->ops = &si1145_trigger_ops;
  1106. iio_trigger_set_drvdata(trig, indio_dev);
  1107. ret = devm_request_irq(&client->dev, client->irq,
  1108. iio_trigger_generic_data_rdy_poll,
  1109. IRQF_TRIGGER_FALLING,
  1110. "si1145_irq",
  1111. trig);
  1112. if (ret < 0) {
  1113. dev_err(&client->dev, "irq request failed\n");
  1114. return ret;
  1115. }
  1116. ret = iio_trigger_register(trig);
  1117. if (ret)
  1118. return ret;
  1119. data->trig = trig;
  1120. indio_dev->trig = iio_trigger_get(data->trig);
  1121. return 0;
  1122. }
  1123. static void si1145_remove_trigger(struct iio_dev *indio_dev)
  1124. {
  1125. struct si1145_data *data = iio_priv(indio_dev);
  1126. if (data->trig) {
  1127. iio_trigger_unregister(data->trig);
  1128. data->trig = NULL;
  1129. }
  1130. }
  1131. static int si1145_probe(struct i2c_client *client,
  1132. const struct i2c_device_id *id)
  1133. {
  1134. struct si1145_data *data;
  1135. struct iio_dev *indio_dev;
  1136. u8 part_id, rev_id, seq_id;
  1137. int ret;
  1138. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  1139. if (!indio_dev)
  1140. return -ENOMEM;
  1141. data = iio_priv(indio_dev);
  1142. i2c_set_clientdata(client, indio_dev);
  1143. data->client = client;
  1144. data->part_info = &si1145_part_info[id->driver_data];
  1145. part_id = ret = i2c_smbus_read_byte_data(data->client,
  1146. SI1145_REG_PART_ID);
  1147. if (ret < 0)
  1148. return ret;
  1149. rev_id = ret = i2c_smbus_read_byte_data(data->client,
  1150. SI1145_REG_REV_ID);
  1151. if (ret < 0)
  1152. return ret;
  1153. seq_id = ret = i2c_smbus_read_byte_data(data->client,
  1154. SI1145_REG_SEQ_ID);
  1155. if (ret < 0)
  1156. return ret;
  1157. dev_info(&client->dev, "device ID part %#02hhx rev %#02hhx seq %#02hhx\n",
  1158. part_id, rev_id, seq_id);
  1159. if (part_id != data->part_info->part) {
  1160. dev_err(&client->dev, "part ID mismatch got %#02hhx, expected %#02x\n",
  1161. part_id, data->part_info->part);
  1162. return -ENODEV;
  1163. }
  1164. indio_dev->dev.parent = &client->dev;
  1165. indio_dev->name = id->name;
  1166. indio_dev->channels = data->part_info->channels;
  1167. indio_dev->num_channels = data->part_info->num_channels;
  1168. indio_dev->info = data->part_info->iio_info;
  1169. indio_dev->modes = INDIO_DIRECT_MODE;
  1170. mutex_init(&data->lock);
  1171. mutex_init(&data->cmdlock);
  1172. ret = si1145_initialize(data);
  1173. if (ret < 0)
  1174. return ret;
  1175. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  1176. si1145_trigger_handler, &si1145_buffer_setup_ops);
  1177. if (ret < 0)
  1178. return ret;
  1179. if (client->irq) {
  1180. ret = si1145_probe_trigger(indio_dev);
  1181. if (ret < 0)
  1182. goto error_free_buffer;
  1183. } else {
  1184. dev_info(&client->dev, "no irq, using polling\n");
  1185. }
  1186. ret = iio_device_register(indio_dev);
  1187. if (ret < 0)
  1188. goto error_free_trigger;
  1189. return 0;
  1190. error_free_trigger:
  1191. si1145_remove_trigger(indio_dev);
  1192. error_free_buffer:
  1193. iio_triggered_buffer_cleanup(indio_dev);
  1194. return ret;
  1195. }
  1196. static const struct i2c_device_id si1145_ids[] = {
  1197. { "si1132", SI1132 },
  1198. { "si1141", SI1141 },
  1199. { "si1142", SI1142 },
  1200. { "si1143", SI1143 },
  1201. { "si1145", SI1145 },
  1202. { "si1146", SI1146 },
  1203. { "si1147", SI1147 },
  1204. { }
  1205. };
  1206. MODULE_DEVICE_TABLE(i2c, si1145_ids);
  1207. static int si1145_remove(struct i2c_client *client)
  1208. {
  1209. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  1210. iio_device_unregister(indio_dev);
  1211. si1145_remove_trigger(indio_dev);
  1212. iio_triggered_buffer_cleanup(indio_dev);
  1213. return 0;
  1214. }
  1215. static struct i2c_driver si1145_driver = {
  1216. .driver = {
  1217. .name = "si1145",
  1218. },
  1219. .probe = si1145_probe,
  1220. .remove = si1145_remove,
  1221. .id_table = si1145_ids,
  1222. };
  1223. module_i2c_driver(si1145_driver);
  1224. MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
  1225. MODULE_DESCRIPTION("Silabs SI1132 and SI1141/2/3/5/6/7 proximity, ambient light and UV index sensor driver");
  1226. MODULE_LICENSE("GPL");