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. * @buffer: Used to pack data read from sensor.
  158. */
  159. struct si1145_data {
  160. struct i2c_client *client;
  161. struct mutex lock;
  162. struct mutex cmdlock;
  163. int rsp_seq;
  164. const struct si1145_part_info *part_info;
  165. unsigned long scan_mask;
  166. bool autonomous;
  167. struct iio_trigger *trig;
  168. int meas_rate;
  169. /*
  170. * Ensure timestamp will be naturally aligned if present.
  171. * Maximum buffer size (may be only partly used if not all
  172. * channels are enabled):
  173. * 6*2 bytes channels data + 4 bytes alignment +
  174. * 8 bytes timestamp
  175. */
  176. u8 buffer[24] __aligned(8);
  177. };
  178. /**
  179. * __si1145_command_reset() - Send CMD_NOP and wait for response 0
  180. *
  181. * Does not modify data->rsp_seq
  182. *
  183. * Return: 0 on success and -errno on error.
  184. */
  185. static int __si1145_command_reset(struct si1145_data *data)
  186. {
  187. struct device *dev = &data->client->dev;
  188. unsigned long stop_jiffies;
  189. int ret;
  190. ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND,
  191. SI1145_CMD_NOP);
  192. if (ret < 0)
  193. return ret;
  194. msleep(SI1145_COMMAND_MINSLEEP_MS);
  195. stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
  196. while (true) {
  197. ret = i2c_smbus_read_byte_data(data->client,
  198. SI1145_REG_RESPONSE);
  199. if (ret <= 0)
  200. return ret;
  201. if (time_after(jiffies, stop_jiffies)) {
  202. dev_warn(dev, "timeout on reset\n");
  203. return -ETIMEDOUT;
  204. }
  205. msleep(SI1145_COMMAND_MINSLEEP_MS);
  206. continue;
  207. }
  208. }
  209. /**
  210. * si1145_command() - Execute a command and poll the response register
  211. *
  212. * All conversion overflows are reported as -EOVERFLOW
  213. * INVALID_SETTING is reported as -EINVAL
  214. * Timeouts are reported as -ETIMEDOUT
  215. *
  216. * Return: 0 on success or -errno on failure
  217. */
  218. static int si1145_command(struct si1145_data *data, u8 cmd)
  219. {
  220. struct device *dev = &data->client->dev;
  221. unsigned long stop_jiffies;
  222. int ret;
  223. mutex_lock(&data->cmdlock);
  224. if (data->rsp_seq < 0) {
  225. ret = __si1145_command_reset(data);
  226. if (ret < 0) {
  227. dev_err(dev, "failed to reset command counter, ret=%d\n",
  228. ret);
  229. goto out;
  230. }
  231. data->rsp_seq = 0;
  232. }
  233. ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND, cmd);
  234. if (ret) {
  235. dev_warn(dev, "failed to write command, ret=%d\n", ret);
  236. goto out;
  237. }
  238. /* Sleep a little to ensure the command is received */
  239. msleep(SI1145_COMMAND_MINSLEEP_MS);
  240. stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
  241. while (true) {
  242. ret = i2c_smbus_read_byte_data(data->client,
  243. SI1145_REG_RESPONSE);
  244. if (ret < 0) {
  245. dev_warn(dev, "failed to read response, ret=%d\n", ret);
  246. break;
  247. }
  248. if ((ret & ~SI1145_RSP_COUNTER_MASK) == 0) {
  249. if (ret == data->rsp_seq) {
  250. if (time_after(jiffies, stop_jiffies)) {
  251. dev_warn(dev, "timeout on command %#02hhx\n",
  252. cmd);
  253. ret = -ETIMEDOUT;
  254. break;
  255. }
  256. msleep(SI1145_COMMAND_MINSLEEP_MS);
  257. continue;
  258. }
  259. if (ret == ((data->rsp_seq + 1) &
  260. SI1145_RSP_COUNTER_MASK)) {
  261. data->rsp_seq = ret;
  262. ret = 0;
  263. break;
  264. }
  265. dev_warn(dev, "unexpected response counter %d instead of %d\n",
  266. ret, (data->rsp_seq + 1) &
  267. SI1145_RSP_COUNTER_MASK);
  268. ret = -EIO;
  269. } else {
  270. if (ret == SI1145_RSP_INVALID_SETTING) {
  271. dev_warn(dev, "INVALID_SETTING error on command %#02hhx\n",
  272. cmd);
  273. ret = -EINVAL;
  274. } else {
  275. /* All overflows are treated identically */
  276. dev_dbg(dev, "overflow, ret=%d, cmd=%#02hhx\n",
  277. ret, cmd);
  278. ret = -EOVERFLOW;
  279. }
  280. }
  281. /* Force a counter reset next time */
  282. data->rsp_seq = -1;
  283. break;
  284. }
  285. out:
  286. mutex_unlock(&data->cmdlock);
  287. return ret;
  288. }
  289. static int si1145_param_update(struct si1145_data *data, u8 op, u8 param,
  290. u8 value)
  291. {
  292. int ret;
  293. ret = i2c_smbus_write_byte_data(data->client,
  294. SI1145_REG_PARAM_WR, value);
  295. if (ret < 0)
  296. return ret;
  297. return si1145_command(data, op | (param & 0x1F));
  298. }
  299. static int si1145_param_set(struct si1145_data *data, u8 param, u8 value)
  300. {
  301. return si1145_param_update(data, SI1145_CMD_PARAM_SET, param, value);
  302. }
  303. /* Set param. Returns negative errno or current value */
  304. static int si1145_param_query(struct si1145_data *data, u8 param)
  305. {
  306. int ret;
  307. ret = si1145_command(data, SI1145_CMD_PARAM_QUERY | (param & 0x1F));
  308. if (ret < 0)
  309. return ret;
  310. return i2c_smbus_read_byte_data(data->client, SI1145_REG_PARAM_RD);
  311. }
  312. /* Expand 8 bit compressed value to 16 bit, see Silabs AN498 */
  313. static u16 si1145_uncompress(u8 x)
  314. {
  315. u16 result = 0;
  316. u8 exponent = 0;
  317. if (x < 8)
  318. return 0;
  319. exponent = (x & 0xf0) >> 4;
  320. result = 0x10 | (x & 0x0f);
  321. if (exponent >= 4)
  322. return result << (exponent - 4);
  323. return result >> (4 - exponent);
  324. }
  325. /* Compress 16 bit value to 8 bit, see Silabs AN498 */
  326. static u8 si1145_compress(u16 x)
  327. {
  328. u32 exponent = 0;
  329. u32 significand = 0;
  330. u32 tmp = x;
  331. if (x == 0x0000)
  332. return 0x00;
  333. if (x == 0x0001)
  334. return 0x08;
  335. while (1) {
  336. tmp >>= 1;
  337. exponent += 1;
  338. if (tmp == 1)
  339. break;
  340. }
  341. if (exponent < 5) {
  342. significand = x << (4 - exponent);
  343. return (exponent << 4) | (significand & 0xF);
  344. }
  345. significand = x >> (exponent - 5);
  346. if (significand & 1) {
  347. significand += 2;
  348. if (significand & 0x0040) {
  349. exponent += 1;
  350. significand >>= 1;
  351. }
  352. }
  353. return (exponent << 4) | ((significand >> 1) & 0xF);
  354. }
  355. /* Write meas_rate in hardware */
  356. static int si1145_set_meas_rate(struct si1145_data *data, int interval)
  357. {
  358. if (data->part_info->uncompressed_meas_rate)
  359. return i2c_smbus_write_word_data(data->client,
  360. SI1145_REG_MEAS_RATE, interval);
  361. else
  362. return i2c_smbus_write_byte_data(data->client,
  363. SI1145_REG_MEAS_RATE, interval);
  364. }
  365. static int si1145_read_samp_freq(struct si1145_data *data, int *val, int *val2)
  366. {
  367. *val = 32000;
  368. if (data->part_info->uncompressed_meas_rate)
  369. *val2 = data->meas_rate;
  370. else
  371. *val2 = si1145_uncompress(data->meas_rate);
  372. return IIO_VAL_FRACTIONAL;
  373. }
  374. /* Set the samp freq in driver private data */
  375. static int si1145_store_samp_freq(struct si1145_data *data, int val)
  376. {
  377. int ret = 0;
  378. int meas_rate;
  379. if (val <= 0 || val > 32000)
  380. return -ERANGE;
  381. meas_rate = 32000 / val;
  382. mutex_lock(&data->lock);
  383. if (data->autonomous) {
  384. ret = si1145_set_meas_rate(data, meas_rate);
  385. if (ret)
  386. goto out;
  387. }
  388. if (data->part_info->uncompressed_meas_rate)
  389. data->meas_rate = meas_rate;
  390. else
  391. data->meas_rate = si1145_compress(meas_rate);
  392. out:
  393. mutex_unlock(&data->lock);
  394. return ret;
  395. }
  396. static irqreturn_t si1145_trigger_handler(int irq, void *private)
  397. {
  398. struct iio_poll_func *pf = private;
  399. struct iio_dev *indio_dev = pf->indio_dev;
  400. struct si1145_data *data = iio_priv(indio_dev);
  401. int i, j = 0;
  402. int ret;
  403. u8 irq_status = 0;
  404. if (!data->autonomous) {
  405. ret = si1145_command(data, SI1145_CMD_PSALS_FORCE);
  406. if (ret < 0 && ret != -EOVERFLOW)
  407. goto done;
  408. } else {
  409. irq_status = ret = i2c_smbus_read_byte_data(data->client,
  410. SI1145_REG_IRQ_STATUS);
  411. if (ret < 0)
  412. goto done;
  413. if (!(irq_status & SI1145_MASK_ALL_IE))
  414. goto done;
  415. }
  416. for_each_set_bit(i, indio_dev->active_scan_mask,
  417. indio_dev->masklength) {
  418. int run = 1;
  419. while (i + run < indio_dev->masklength) {
  420. if (!test_bit(i + run, indio_dev->active_scan_mask))
  421. break;
  422. if (indio_dev->channels[i + run].address !=
  423. indio_dev->channels[i].address + 2 * run)
  424. break;
  425. run++;
  426. }
  427. ret = i2c_smbus_read_i2c_block_data_or_emulated(
  428. data->client, indio_dev->channels[i].address,
  429. sizeof(u16) * run, &data->buffer[j]);
  430. if (ret < 0)
  431. goto done;
  432. j += run * sizeof(u16);
  433. i += run - 1;
  434. }
  435. if (data->autonomous) {
  436. ret = i2c_smbus_write_byte_data(data->client,
  437. SI1145_REG_IRQ_STATUS,
  438. irq_status & SI1145_MASK_ALL_IE);
  439. if (ret < 0)
  440. goto done;
  441. }
  442. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  443. iio_get_time_ns(indio_dev));
  444. done:
  445. iio_trigger_notify_done(indio_dev->trig);
  446. return IRQ_HANDLED;
  447. }
  448. static int si1145_set_chlist(struct iio_dev *indio_dev, unsigned long scan_mask)
  449. {
  450. struct si1145_data *data = iio_priv(indio_dev);
  451. u8 reg = 0, mux;
  452. int ret;
  453. int i;
  454. /* channel list already set, no need to reprogram */
  455. if (data->scan_mask == scan_mask)
  456. return 0;
  457. for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
  458. switch (indio_dev->channels[i].address) {
  459. case SI1145_REG_ALSVIS_DATA:
  460. reg |= SI1145_CHLIST_EN_ALSVIS;
  461. break;
  462. case SI1145_REG_ALSIR_DATA:
  463. reg |= SI1145_CHLIST_EN_ALSIR;
  464. break;
  465. case SI1145_REG_PS1_DATA:
  466. reg |= SI1145_CHLIST_EN_PS1;
  467. break;
  468. case SI1145_REG_PS2_DATA:
  469. reg |= SI1145_CHLIST_EN_PS2;
  470. break;
  471. case SI1145_REG_PS3_DATA:
  472. reg |= SI1145_CHLIST_EN_PS3;
  473. break;
  474. case SI1145_REG_AUX_DATA:
  475. switch (indio_dev->channels[i].type) {
  476. case IIO_UVINDEX:
  477. reg |= SI1145_CHLIST_EN_UV;
  478. break;
  479. default:
  480. reg |= SI1145_CHLIST_EN_AUX;
  481. if (indio_dev->channels[i].type == IIO_TEMP)
  482. mux = SI1145_MUX_TEMP;
  483. else
  484. mux = SI1145_MUX_VDD;
  485. ret = si1145_param_set(data,
  486. SI1145_PARAM_AUX_ADC_MUX, mux);
  487. if (ret < 0)
  488. return ret;
  489. break;
  490. }
  491. }
  492. }
  493. data->scan_mask = scan_mask;
  494. ret = si1145_param_set(data, SI1145_PARAM_CHLIST, reg);
  495. return ret < 0 ? ret : 0;
  496. }
  497. static int si1145_measure(struct iio_dev *indio_dev,
  498. struct iio_chan_spec const *chan)
  499. {
  500. struct si1145_data *data = iio_priv(indio_dev);
  501. u8 cmd;
  502. int ret;
  503. ret = si1145_set_chlist(indio_dev, BIT(chan->scan_index));
  504. if (ret < 0)
  505. return ret;
  506. cmd = (chan->type == IIO_PROXIMITY) ? SI1145_CMD_PS_FORCE :
  507. SI1145_CMD_ALS_FORCE;
  508. ret = si1145_command(data, cmd);
  509. if (ret < 0 && ret != -EOVERFLOW)
  510. return ret;
  511. return i2c_smbus_read_word_data(data->client, chan->address);
  512. }
  513. /*
  514. * Conversion between iio scale and ADC_GAIN values
  515. * These could be further adjusted but proximity/intensity are dimensionless
  516. */
  517. static const int si1145_proximity_scale_available[] = {
  518. 128, 64, 32, 16, 8, 4};
  519. static const int si1145_intensity_scale_available[] = {
  520. 128, 64, 32, 16, 8, 4, 2, 1};
  521. static IIO_CONST_ATTR(in_proximity_scale_available,
  522. "128 64 32 16 8 4");
  523. static IIO_CONST_ATTR(in_intensity_scale_available,
  524. "128 64 32 16 8 4 2 1");
  525. static IIO_CONST_ATTR(in_intensity_ir_scale_available,
  526. "128 64 32 16 8 4 2 1");
  527. static int si1145_scale_from_adcgain(int regval)
  528. {
  529. return 128 >> regval;
  530. }
  531. static int si1145_proximity_adcgain_from_scale(int val, int val2)
  532. {
  533. val = find_closest_descending(val, si1145_proximity_scale_available,
  534. ARRAY_SIZE(si1145_proximity_scale_available));
  535. if (val < 0 || val > 5 || val2 != 0)
  536. return -EINVAL;
  537. return val;
  538. }
  539. static int si1145_intensity_adcgain_from_scale(int val, int val2)
  540. {
  541. val = find_closest_descending(val, si1145_intensity_scale_available,
  542. ARRAY_SIZE(si1145_intensity_scale_available));
  543. if (val < 0 || val > 7 || val2 != 0)
  544. return -EINVAL;
  545. return val;
  546. }
  547. static int si1145_read_raw(struct iio_dev *indio_dev,
  548. struct iio_chan_spec const *chan,
  549. int *val, int *val2, long mask)
  550. {
  551. struct si1145_data *data = iio_priv(indio_dev);
  552. int ret;
  553. u8 reg;
  554. switch (mask) {
  555. case IIO_CHAN_INFO_RAW:
  556. switch (chan->type) {
  557. case IIO_INTENSITY:
  558. case IIO_PROXIMITY:
  559. case IIO_VOLTAGE:
  560. case IIO_TEMP:
  561. case IIO_UVINDEX:
  562. ret = iio_device_claim_direct_mode(indio_dev);
  563. if (ret)
  564. return ret;
  565. ret = si1145_measure(indio_dev, chan);
  566. iio_device_release_direct_mode(indio_dev);
  567. if (ret < 0)
  568. return ret;
  569. *val = ret;
  570. return IIO_VAL_INT;
  571. case IIO_CURRENT:
  572. ret = i2c_smbus_read_byte_data(data->client,
  573. SI1145_PS_LED_REG(chan->channel));
  574. if (ret < 0)
  575. return ret;
  576. *val = (ret >> SI1145_PS_LED_SHIFT(chan->channel))
  577. & 0x0f;
  578. return IIO_VAL_INT;
  579. default:
  580. return -EINVAL;
  581. }
  582. case IIO_CHAN_INFO_SCALE:
  583. switch (chan->type) {
  584. case IIO_PROXIMITY:
  585. reg = SI1145_PARAM_PS_ADC_GAIN;
  586. break;
  587. case IIO_INTENSITY:
  588. if (chan->channel2 == IIO_MOD_LIGHT_IR)
  589. reg = SI1145_PARAM_ALSIR_ADC_GAIN;
  590. else
  591. reg = SI1145_PARAM_ALSVIS_ADC_GAIN;
  592. break;
  593. case IIO_TEMP:
  594. *val = 28;
  595. *val2 = 571429;
  596. return IIO_VAL_INT_PLUS_MICRO;
  597. case IIO_UVINDEX:
  598. *val = 0;
  599. *val2 = 10000;
  600. return IIO_VAL_INT_PLUS_MICRO;
  601. default:
  602. return -EINVAL;
  603. }
  604. ret = si1145_param_query(data, reg);
  605. if (ret < 0)
  606. return ret;
  607. *val = si1145_scale_from_adcgain(ret & 0x07);
  608. return IIO_VAL_INT;
  609. case IIO_CHAN_INFO_OFFSET:
  610. switch (chan->type) {
  611. case IIO_TEMP:
  612. /*
  613. * -ADC offset - ADC counts @ 25°C -
  614. * 35 * ADC counts / °C
  615. */
  616. *val = -256 - 11136 + 25 * 35;
  617. return IIO_VAL_INT;
  618. default:
  619. /*
  620. * All ADC measurements have are by default offset
  621. * by -256
  622. * See AN498 5.6.3
  623. */
  624. ret = si1145_param_query(data, SI1145_PARAM_ADC_OFFSET);
  625. if (ret < 0)
  626. return ret;
  627. *val = -si1145_uncompress(ret);
  628. return IIO_VAL_INT;
  629. }
  630. case IIO_CHAN_INFO_SAMP_FREQ:
  631. return si1145_read_samp_freq(data, val, val2);
  632. default:
  633. return -EINVAL;
  634. }
  635. }
  636. static int si1145_write_raw(struct iio_dev *indio_dev,
  637. struct iio_chan_spec const *chan,
  638. int val, int val2, long mask)
  639. {
  640. struct si1145_data *data = iio_priv(indio_dev);
  641. u8 reg1, reg2, shift;
  642. int ret;
  643. switch (mask) {
  644. case IIO_CHAN_INFO_SCALE:
  645. switch (chan->type) {
  646. case IIO_PROXIMITY:
  647. val = si1145_proximity_adcgain_from_scale(val, val2);
  648. if (val < 0)
  649. return val;
  650. reg1 = SI1145_PARAM_PS_ADC_GAIN;
  651. reg2 = SI1145_PARAM_PS_ADC_COUNTER;
  652. break;
  653. case IIO_INTENSITY:
  654. val = si1145_intensity_adcgain_from_scale(val, val2);
  655. if (val < 0)
  656. return val;
  657. if (chan->channel2 == IIO_MOD_LIGHT_IR) {
  658. reg1 = SI1145_PARAM_ALSIR_ADC_GAIN;
  659. reg2 = SI1145_PARAM_ALSIR_ADC_COUNTER;
  660. } else {
  661. reg1 = SI1145_PARAM_ALSVIS_ADC_GAIN;
  662. reg2 = SI1145_PARAM_ALSVIS_ADC_COUNTER;
  663. }
  664. break;
  665. default:
  666. return -EINVAL;
  667. }
  668. ret = iio_device_claim_direct_mode(indio_dev);
  669. if (ret)
  670. return ret;
  671. ret = si1145_param_set(data, reg1, val);
  672. if (ret < 0) {
  673. iio_device_release_direct_mode(indio_dev);
  674. return ret;
  675. }
  676. /* Set recovery period to one's complement of gain */
  677. ret = si1145_param_set(data, reg2, (~val & 0x07) << 4);
  678. iio_device_release_direct_mode(indio_dev);
  679. return ret;
  680. case IIO_CHAN_INFO_RAW:
  681. if (chan->type != IIO_CURRENT)
  682. return -EINVAL;
  683. if (val < 0 || val > 15 || val2 != 0)
  684. return -EINVAL;
  685. reg1 = SI1145_PS_LED_REG(chan->channel);
  686. shift = SI1145_PS_LED_SHIFT(chan->channel);
  687. ret = iio_device_claim_direct_mode(indio_dev);
  688. if (ret)
  689. return ret;
  690. ret = i2c_smbus_read_byte_data(data->client, reg1);
  691. if (ret < 0) {
  692. iio_device_release_direct_mode(indio_dev);
  693. return ret;
  694. }
  695. ret = i2c_smbus_write_byte_data(data->client, reg1,
  696. (ret & ~(0x0f << shift)) |
  697. ((val & 0x0f) << shift));
  698. iio_device_release_direct_mode(indio_dev);
  699. return ret;
  700. case IIO_CHAN_INFO_SAMP_FREQ:
  701. return si1145_store_samp_freq(data, val);
  702. default:
  703. return -EINVAL;
  704. }
  705. }
  706. #define SI1145_ST { \
  707. .sign = 'u', \
  708. .realbits = 16, \
  709. .storagebits = 16, \
  710. .endianness = IIO_LE, \
  711. }
  712. #define SI1145_INTENSITY_CHANNEL(_si) { \
  713. .type = IIO_INTENSITY, \
  714. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  715. BIT(IIO_CHAN_INFO_OFFSET) | \
  716. BIT(IIO_CHAN_INFO_SCALE), \
  717. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  718. .scan_type = SI1145_ST, \
  719. .scan_index = _si, \
  720. .address = SI1145_REG_ALSVIS_DATA, \
  721. }
  722. #define SI1145_INTENSITY_IR_CHANNEL(_si) { \
  723. .type = IIO_INTENSITY, \
  724. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  725. BIT(IIO_CHAN_INFO_OFFSET) | \
  726. BIT(IIO_CHAN_INFO_SCALE), \
  727. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  728. .modified = 1, \
  729. .channel2 = IIO_MOD_LIGHT_IR, \
  730. .scan_type = SI1145_ST, \
  731. .scan_index = _si, \
  732. .address = SI1145_REG_ALSIR_DATA, \
  733. }
  734. #define SI1145_TEMP_CHANNEL(_si) { \
  735. .type = IIO_TEMP, \
  736. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  737. BIT(IIO_CHAN_INFO_OFFSET) | \
  738. BIT(IIO_CHAN_INFO_SCALE), \
  739. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  740. .scan_type = SI1145_ST, \
  741. .scan_index = _si, \
  742. .address = SI1145_REG_AUX_DATA, \
  743. }
  744. #define SI1145_UV_CHANNEL(_si) { \
  745. .type = IIO_UVINDEX, \
  746. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  747. BIT(IIO_CHAN_INFO_SCALE), \
  748. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  749. .scan_type = SI1145_ST, \
  750. .scan_index = _si, \
  751. .address = SI1145_REG_AUX_DATA, \
  752. }
  753. #define SI1145_PROXIMITY_CHANNEL(_si, _ch) { \
  754. .type = IIO_PROXIMITY, \
  755. .indexed = 1, \
  756. .channel = _ch, \
  757. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  758. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  759. BIT(IIO_CHAN_INFO_OFFSET), \
  760. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  761. .scan_type = SI1145_ST, \
  762. .scan_index = _si, \
  763. .address = SI1145_REG_PS1_DATA + _ch * 2, \
  764. }
  765. #define SI1145_VOLTAGE_CHANNEL(_si) { \
  766. .type = IIO_VOLTAGE, \
  767. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  768. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  769. .scan_type = SI1145_ST, \
  770. .scan_index = _si, \
  771. .address = SI1145_REG_AUX_DATA, \
  772. }
  773. #define SI1145_CURRENT_CHANNEL(_ch) { \
  774. .type = IIO_CURRENT, \
  775. .indexed = 1, \
  776. .channel = _ch, \
  777. .output = 1, \
  778. .scan_index = -1, \
  779. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  780. }
  781. static const struct iio_chan_spec si1132_channels[] = {
  782. SI1145_INTENSITY_CHANNEL(0),
  783. SI1145_INTENSITY_IR_CHANNEL(1),
  784. SI1145_TEMP_CHANNEL(2),
  785. SI1145_VOLTAGE_CHANNEL(3),
  786. SI1145_UV_CHANNEL(4),
  787. IIO_CHAN_SOFT_TIMESTAMP(6),
  788. };
  789. static const struct iio_chan_spec si1141_channels[] = {
  790. SI1145_INTENSITY_CHANNEL(0),
  791. SI1145_INTENSITY_IR_CHANNEL(1),
  792. SI1145_PROXIMITY_CHANNEL(2, 0),
  793. SI1145_TEMP_CHANNEL(3),
  794. SI1145_VOLTAGE_CHANNEL(4),
  795. IIO_CHAN_SOFT_TIMESTAMP(5),
  796. SI1145_CURRENT_CHANNEL(0),
  797. };
  798. static const struct iio_chan_spec si1142_channels[] = {
  799. SI1145_INTENSITY_CHANNEL(0),
  800. SI1145_INTENSITY_IR_CHANNEL(1),
  801. SI1145_PROXIMITY_CHANNEL(2, 0),
  802. SI1145_PROXIMITY_CHANNEL(3, 1),
  803. SI1145_TEMP_CHANNEL(4),
  804. SI1145_VOLTAGE_CHANNEL(5),
  805. IIO_CHAN_SOFT_TIMESTAMP(6),
  806. SI1145_CURRENT_CHANNEL(0),
  807. SI1145_CURRENT_CHANNEL(1),
  808. };
  809. static const struct iio_chan_spec si1143_channels[] = {
  810. SI1145_INTENSITY_CHANNEL(0),
  811. SI1145_INTENSITY_IR_CHANNEL(1),
  812. SI1145_PROXIMITY_CHANNEL(2, 0),
  813. SI1145_PROXIMITY_CHANNEL(3, 1),
  814. SI1145_PROXIMITY_CHANNEL(4, 2),
  815. SI1145_TEMP_CHANNEL(5),
  816. SI1145_VOLTAGE_CHANNEL(6),
  817. IIO_CHAN_SOFT_TIMESTAMP(7),
  818. SI1145_CURRENT_CHANNEL(0),
  819. SI1145_CURRENT_CHANNEL(1),
  820. SI1145_CURRENT_CHANNEL(2),
  821. };
  822. static const struct iio_chan_spec si1145_channels[] = {
  823. SI1145_INTENSITY_CHANNEL(0),
  824. SI1145_INTENSITY_IR_CHANNEL(1),
  825. SI1145_PROXIMITY_CHANNEL(2, 0),
  826. SI1145_TEMP_CHANNEL(3),
  827. SI1145_VOLTAGE_CHANNEL(4),
  828. SI1145_UV_CHANNEL(5),
  829. IIO_CHAN_SOFT_TIMESTAMP(6),
  830. SI1145_CURRENT_CHANNEL(0),
  831. };
  832. static const struct iio_chan_spec si1146_channels[] = {
  833. SI1145_INTENSITY_CHANNEL(0),
  834. SI1145_INTENSITY_IR_CHANNEL(1),
  835. SI1145_TEMP_CHANNEL(2),
  836. SI1145_VOLTAGE_CHANNEL(3),
  837. SI1145_UV_CHANNEL(4),
  838. SI1145_PROXIMITY_CHANNEL(5, 0),
  839. SI1145_PROXIMITY_CHANNEL(6, 1),
  840. IIO_CHAN_SOFT_TIMESTAMP(7),
  841. SI1145_CURRENT_CHANNEL(0),
  842. SI1145_CURRENT_CHANNEL(1),
  843. };
  844. static const struct iio_chan_spec si1147_channels[] = {
  845. SI1145_INTENSITY_CHANNEL(0),
  846. SI1145_INTENSITY_IR_CHANNEL(1),
  847. SI1145_PROXIMITY_CHANNEL(2, 0),
  848. SI1145_PROXIMITY_CHANNEL(3, 1),
  849. SI1145_PROXIMITY_CHANNEL(4, 2),
  850. SI1145_TEMP_CHANNEL(5),
  851. SI1145_VOLTAGE_CHANNEL(6),
  852. SI1145_UV_CHANNEL(7),
  853. IIO_CHAN_SOFT_TIMESTAMP(8),
  854. SI1145_CURRENT_CHANNEL(0),
  855. SI1145_CURRENT_CHANNEL(1),
  856. SI1145_CURRENT_CHANNEL(2),
  857. };
  858. static struct attribute *si1132_attributes[] = {
  859. &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
  860. &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
  861. NULL,
  862. };
  863. static struct attribute *si114x_attributes[] = {
  864. &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
  865. &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
  866. &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
  867. NULL,
  868. };
  869. static const struct attribute_group si1132_attribute_group = {
  870. .attrs = si1132_attributes,
  871. };
  872. static const struct attribute_group si114x_attribute_group = {
  873. .attrs = si114x_attributes,
  874. };
  875. static const struct iio_info si1132_info = {
  876. .read_raw = si1145_read_raw,
  877. .write_raw = si1145_write_raw,
  878. .attrs = &si1132_attribute_group,
  879. };
  880. static const struct iio_info si114x_info = {
  881. .read_raw = si1145_read_raw,
  882. .write_raw = si1145_write_raw,
  883. .attrs = &si114x_attribute_group,
  884. };
  885. #define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \
  886. {id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate}
  887. static const struct si1145_part_info si1145_part_info[] = {
  888. [SI1132] = SI1145_PART(0x32, &si1132_info, si1132_channels, 0, true),
  889. [SI1141] = SI1145_PART(0x41, &si114x_info, si1141_channels, 1, false),
  890. [SI1142] = SI1145_PART(0x42, &si114x_info, si1142_channels, 2, false),
  891. [SI1143] = SI1145_PART(0x43, &si114x_info, si1143_channels, 3, false),
  892. [SI1145] = SI1145_PART(0x45, &si114x_info, si1145_channels, 1, true),
  893. [SI1146] = SI1145_PART(0x46, &si114x_info, si1146_channels, 2, true),
  894. [SI1147] = SI1145_PART(0x47, &si114x_info, si1147_channels, 3, true),
  895. };
  896. static int si1145_initialize(struct si1145_data *data)
  897. {
  898. struct i2c_client *client = data->client;
  899. int ret;
  900. ret = i2c_smbus_write_byte_data(client, SI1145_REG_COMMAND,
  901. SI1145_CMD_RESET);
  902. if (ret < 0)
  903. return ret;
  904. msleep(SI1145_COMMAND_TIMEOUT_MS);
  905. /* Hardware key, magic value */
  906. ret = i2c_smbus_write_byte_data(client, SI1145_REG_HW_KEY, 0x17);
  907. if (ret < 0)
  908. return ret;
  909. msleep(SI1145_COMMAND_TIMEOUT_MS);
  910. /* Turn off autonomous mode */
  911. ret = si1145_set_meas_rate(data, 0);
  912. if (ret < 0)
  913. return ret;
  914. /* Initialize sampling freq to 10 Hz */
  915. ret = si1145_store_samp_freq(data, 10);
  916. if (ret < 0)
  917. return ret;
  918. /* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */
  919. switch (data->part_info->num_leds) {
  920. case 3:
  921. ret = i2c_smbus_write_byte_data(client,
  922. SI1145_REG_PS_LED3,
  923. SI1145_LED_CURRENT_45mA);
  924. if (ret < 0)
  925. return ret;
  926. /* fallthrough */
  927. case 2:
  928. ret = i2c_smbus_write_byte_data(client,
  929. SI1145_REG_PS_LED21,
  930. (SI1145_LED_CURRENT_45mA << 4) |
  931. SI1145_LED_CURRENT_45mA);
  932. break;
  933. case 1:
  934. ret = i2c_smbus_write_byte_data(client,
  935. SI1145_REG_PS_LED21,
  936. SI1145_LED_CURRENT_45mA);
  937. break;
  938. default:
  939. ret = 0;
  940. break;
  941. }
  942. if (ret < 0)
  943. return ret;
  944. /* Set normal proximity measurement mode */
  945. ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_MISC,
  946. SI1145_PS_ADC_MODE_NORMAL);
  947. if (ret < 0)
  948. return ret;
  949. ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01);
  950. if (ret < 0)
  951. return ret;
  952. /* ADC_COUNTER should be one complement of ADC_GAIN */
  953. ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_COUNTER, 0x06 << 4);
  954. if (ret < 0)
  955. return ret;
  956. /* Set ALS visible measurement mode */
  957. ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_MISC,
  958. SI1145_ADC_MISC_RANGE);
  959. if (ret < 0)
  960. return ret;
  961. ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03);
  962. if (ret < 0)
  963. return ret;
  964. ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER,
  965. 0x04 << 4);
  966. if (ret < 0)
  967. return ret;
  968. /* Set ALS IR measurement mode */
  969. ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_MISC,
  970. SI1145_ADC_MISC_RANGE);
  971. if (ret < 0)
  972. return ret;
  973. ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01);
  974. if (ret < 0)
  975. return ret;
  976. ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER,
  977. 0x06 << 4);
  978. if (ret < 0)
  979. return ret;
  980. /*
  981. * Initialize UCOEF to default values in datasheet
  982. * These registers are normally zero on reset
  983. */
  984. if (data->part_info == &si1145_part_info[SI1132] ||
  985. data->part_info == &si1145_part_info[SI1145] ||
  986. data->part_info == &si1145_part_info[SI1146] ||
  987. data->part_info == &si1145_part_info[SI1147]) {
  988. ret = i2c_smbus_write_byte_data(data->client,
  989. SI1145_REG_UCOEF1,
  990. SI1145_UCOEF1_DEFAULT);
  991. if (ret < 0)
  992. return ret;
  993. ret = i2c_smbus_write_byte_data(data->client,
  994. SI1145_REG_UCOEF2, SI1145_UCOEF2_DEFAULT);
  995. if (ret < 0)
  996. return ret;
  997. ret = i2c_smbus_write_byte_data(data->client,
  998. SI1145_REG_UCOEF3, SI1145_UCOEF3_DEFAULT);
  999. if (ret < 0)
  1000. return ret;
  1001. ret = i2c_smbus_write_byte_data(data->client,
  1002. SI1145_REG_UCOEF4, SI1145_UCOEF4_DEFAULT);
  1003. if (ret < 0)
  1004. return ret;
  1005. }
  1006. return 0;
  1007. }
  1008. /*
  1009. * Program the channels we want to measure with CMD_PSALS_AUTO. No need for
  1010. * _postdisable as we stop with CMD_PSALS_PAUSE; single measurement (direct)
  1011. * mode reprograms the channels list anyway...
  1012. */
  1013. static int si1145_buffer_preenable(struct iio_dev *indio_dev)
  1014. {
  1015. struct si1145_data *data = iio_priv(indio_dev);
  1016. int ret;
  1017. mutex_lock(&data->lock);
  1018. ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask);
  1019. mutex_unlock(&data->lock);
  1020. return ret;
  1021. }
  1022. static bool si1145_validate_scan_mask(struct iio_dev *indio_dev,
  1023. const unsigned long *scan_mask)
  1024. {
  1025. struct si1145_data *data = iio_priv(indio_dev);
  1026. unsigned int count = 0;
  1027. int i;
  1028. /* Check that at most one AUX channel is enabled */
  1029. for_each_set_bit(i, scan_mask, data->part_info->num_channels) {
  1030. if (indio_dev->channels[i].address == SI1145_REG_AUX_DATA)
  1031. count++;
  1032. }
  1033. return count <= 1;
  1034. }
  1035. static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = {
  1036. .preenable = si1145_buffer_preenable,
  1037. .postenable = iio_triggered_buffer_postenable,
  1038. .predisable = iio_triggered_buffer_predisable,
  1039. .validate_scan_mask = si1145_validate_scan_mask,
  1040. };
  1041. /**
  1042. * si1145_trigger_set_state() - Set trigger state
  1043. *
  1044. * When not using triggers interrupts are disabled and measurement rate is
  1045. * set to zero in order to minimize power consumption.
  1046. */
  1047. static int si1145_trigger_set_state(struct iio_trigger *trig, bool state)
  1048. {
  1049. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  1050. struct si1145_data *data = iio_priv(indio_dev);
  1051. int err = 0, ret;
  1052. mutex_lock(&data->lock);
  1053. if (state) {
  1054. data->autonomous = true;
  1055. err = i2c_smbus_write_byte_data(data->client,
  1056. SI1145_REG_INT_CFG, SI1145_INT_CFG_OE);
  1057. if (err < 0)
  1058. goto disable;
  1059. err = i2c_smbus_write_byte_data(data->client,
  1060. SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE);
  1061. if (err < 0)
  1062. goto disable;
  1063. err = si1145_set_meas_rate(data, data->meas_rate);
  1064. if (err < 0)
  1065. goto disable;
  1066. err = si1145_command(data, SI1145_CMD_PSALS_AUTO);
  1067. if (err < 0)
  1068. goto disable;
  1069. } else {
  1070. disable:
  1071. /* Disable as much as possible skipping errors */
  1072. ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE);
  1073. if (ret < 0 && !err)
  1074. err = ret;
  1075. ret = si1145_set_meas_rate(data, 0);
  1076. if (ret < 0 && !err)
  1077. err = ret;
  1078. ret = i2c_smbus_write_byte_data(data->client,
  1079. SI1145_REG_IRQ_ENABLE, 0);
  1080. if (ret < 0 && !err)
  1081. err = ret;
  1082. ret = i2c_smbus_write_byte_data(data->client,
  1083. SI1145_REG_INT_CFG, 0);
  1084. if (ret < 0 && !err)
  1085. err = ret;
  1086. data->autonomous = false;
  1087. }
  1088. mutex_unlock(&data->lock);
  1089. return err;
  1090. }
  1091. static const struct iio_trigger_ops si1145_trigger_ops = {
  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");