si1145.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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");