si1145.c 34 KB

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