cm36651.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  3. * Author: Beomho Seo <beomho.seo@samsung.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2, as published
  7. * by the Free Software Foundation.
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/i2c.h>
  12. #include <linux/mutex.h>
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/iio/iio.h>
  17. #include <linux/iio/sysfs.h>
  18. #include <linux/iio/events.h>
  19. /* Slave address 0x19 for PS of 7 bit addressing protocol for I2C */
  20. #define CM36651_I2C_ADDR_PS 0x19
  21. /* Alert Response Address */
  22. #define CM36651_ARA 0x0C
  23. /* Ambient light sensor */
  24. #define CM36651_CS_CONF1 0x00
  25. #define CM36651_CS_CONF2 0x01
  26. #define CM36651_ALS_WH_M 0x02
  27. #define CM36651_ALS_WH_L 0x03
  28. #define CM36651_ALS_WL_M 0x04
  29. #define CM36651_ALS_WL_L 0x05
  30. #define CM36651_CS_CONF3 0x06
  31. #define CM36651_CS_CONF_REG_NUM 0x02
  32. /* Proximity sensor */
  33. #define CM36651_PS_CONF1 0x00
  34. #define CM36651_PS_THD 0x01
  35. #define CM36651_PS_CANC 0x02
  36. #define CM36651_PS_CONF2 0x03
  37. #define CM36651_PS_REG_NUM 0x04
  38. /* CS_CONF1 command code */
  39. #define CM36651_ALS_ENABLE 0x00
  40. #define CM36651_ALS_DISABLE 0x01
  41. #define CM36651_ALS_INT_EN 0x02
  42. #define CM36651_ALS_THRES 0x04
  43. /* CS_CONF2 command code */
  44. #define CM36651_CS_CONF2_DEFAULT_BIT 0x08
  45. /* CS_CONF3 channel integration time */
  46. #define CM36651_CS_IT1 0x00 /* Integration time 80 msec */
  47. #define CM36651_CS_IT2 0x40 /* Integration time 160 msec */
  48. #define CM36651_CS_IT3 0x80 /* Integration time 320 msec */
  49. #define CM36651_CS_IT4 0xC0 /* Integration time 640 msec */
  50. /* PS_CONF1 command code */
  51. #define CM36651_PS_ENABLE 0x00
  52. #define CM36651_PS_DISABLE 0x01
  53. #define CM36651_PS_INT_EN 0x02
  54. #define CM36651_PS_PERS2 0x04
  55. #define CM36651_PS_PERS3 0x08
  56. #define CM36651_PS_PERS4 0x0C
  57. /* PS_CONF1 command code: integration time */
  58. #define CM36651_PS_IT1 0x00 /* Integration time 0.32 msec */
  59. #define CM36651_PS_IT2 0x10 /* Integration time 0.42 msec */
  60. #define CM36651_PS_IT3 0x20 /* Integration time 0.52 msec */
  61. #define CM36651_PS_IT4 0x30 /* Integration time 0.64 msec */
  62. /* PS_CONF1 command code: duty ratio */
  63. #define CM36651_PS_DR1 0x00 /* Duty ratio 1/80 */
  64. #define CM36651_PS_DR2 0x40 /* Duty ratio 1/160 */
  65. #define CM36651_PS_DR3 0x80 /* Duty ratio 1/320 */
  66. #define CM36651_PS_DR4 0xC0 /* Duty ratio 1/640 */
  67. /* PS_THD command code */
  68. #define CM36651_PS_INITIAL_THD 0x05
  69. /* PS_CANC command code */
  70. #define CM36651_PS_CANC_DEFAULT 0x00
  71. /* PS_CONF2 command code */
  72. #define CM36651_PS_HYS1 0x00
  73. #define CM36651_PS_HYS2 0x01
  74. #define CM36651_PS_SMART_PERS_EN 0x02
  75. #define CM36651_PS_DIR_INT 0x04
  76. #define CM36651_PS_MS 0x10
  77. #define CM36651_CS_COLOR_NUM 4
  78. #define CM36651_CLOSE_PROXIMITY 0x32
  79. #define CM36651_FAR_PROXIMITY 0x33
  80. #define CM36651_CS_INT_TIME_AVAIL "0.08 0.16 0.32 0.64"
  81. #define CM36651_PS_INT_TIME_AVAIL "0.000320 0.000420 0.000520 0.000640"
  82. enum cm36651_operation_mode {
  83. CM36651_LIGHT_EN,
  84. CM36651_PROXIMITY_EN,
  85. CM36651_PROXIMITY_EV_EN,
  86. };
  87. enum cm36651_light_channel_idx {
  88. CM36651_LIGHT_CHANNEL_IDX_RED,
  89. CM36651_LIGHT_CHANNEL_IDX_GREEN,
  90. CM36651_LIGHT_CHANNEL_IDX_BLUE,
  91. CM36651_LIGHT_CHANNEL_IDX_CLEAR,
  92. };
  93. enum cm36651_command {
  94. CM36651_CMD_READ_RAW_LIGHT,
  95. CM36651_CMD_READ_RAW_PROXIMITY,
  96. CM36651_CMD_PROX_EV_EN,
  97. CM36651_CMD_PROX_EV_DIS,
  98. };
  99. static const u8 cm36651_cs_reg[CM36651_CS_CONF_REG_NUM] = {
  100. CM36651_CS_CONF1,
  101. CM36651_CS_CONF2,
  102. };
  103. static const u8 cm36651_ps_reg[CM36651_PS_REG_NUM] = {
  104. CM36651_PS_CONF1,
  105. CM36651_PS_THD,
  106. CM36651_PS_CANC,
  107. CM36651_PS_CONF2,
  108. };
  109. struct cm36651_data {
  110. const struct cm36651_platform_data *pdata;
  111. struct i2c_client *client;
  112. struct i2c_client *ps_client;
  113. struct i2c_client *ara_client;
  114. struct mutex lock;
  115. struct regulator *vled_reg;
  116. unsigned long flags;
  117. int cs_int_time[CM36651_CS_COLOR_NUM];
  118. int ps_int_time;
  119. u8 cs_ctrl_regs[CM36651_CS_CONF_REG_NUM];
  120. u8 ps_ctrl_regs[CM36651_PS_REG_NUM];
  121. u16 color[CM36651_CS_COLOR_NUM];
  122. };
  123. static int cm36651_setup_reg(struct cm36651_data *cm36651)
  124. {
  125. struct i2c_client *client = cm36651->client;
  126. struct i2c_client *ps_client = cm36651->ps_client;
  127. int i, ret;
  128. /* CS initialization */
  129. cm36651->cs_ctrl_regs[CM36651_CS_CONF1] = CM36651_ALS_ENABLE |
  130. CM36651_ALS_THRES;
  131. cm36651->cs_ctrl_regs[CM36651_CS_CONF2] = CM36651_CS_CONF2_DEFAULT_BIT;
  132. for (i = 0; i < CM36651_CS_CONF_REG_NUM; i++) {
  133. ret = i2c_smbus_write_byte_data(client, cm36651_cs_reg[i],
  134. cm36651->cs_ctrl_regs[i]);
  135. if (ret < 0)
  136. return ret;
  137. }
  138. /* PS initialization */
  139. cm36651->ps_ctrl_regs[CM36651_PS_CONF1] = CM36651_PS_ENABLE |
  140. CM36651_PS_IT2;
  141. cm36651->ps_ctrl_regs[CM36651_PS_THD] = CM36651_PS_INITIAL_THD;
  142. cm36651->ps_ctrl_regs[CM36651_PS_CANC] = CM36651_PS_CANC_DEFAULT;
  143. cm36651->ps_ctrl_regs[CM36651_PS_CONF2] = CM36651_PS_HYS2 |
  144. CM36651_PS_DIR_INT | CM36651_PS_SMART_PERS_EN;
  145. for (i = 0; i < CM36651_PS_REG_NUM; i++) {
  146. ret = i2c_smbus_write_byte_data(ps_client, cm36651_ps_reg[i],
  147. cm36651->ps_ctrl_regs[i]);
  148. if (ret < 0)
  149. return ret;
  150. }
  151. /* Set shutdown mode */
  152. ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
  153. CM36651_ALS_DISABLE);
  154. if (ret < 0)
  155. return ret;
  156. ret = i2c_smbus_write_byte_data(cm36651->ps_client,
  157. CM36651_PS_CONF1, CM36651_PS_DISABLE);
  158. if (ret < 0)
  159. return ret;
  160. return 0;
  161. }
  162. static int cm36651_read_output(struct cm36651_data *cm36651,
  163. struct iio_chan_spec const *chan, int *val)
  164. {
  165. struct i2c_client *client = cm36651->client;
  166. int ret = -EINVAL;
  167. switch (chan->type) {
  168. case IIO_LIGHT:
  169. *val = i2c_smbus_read_word_data(client, chan->address);
  170. if (*val < 0)
  171. return ret;
  172. ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
  173. CM36651_ALS_DISABLE);
  174. if (ret < 0)
  175. return ret;
  176. ret = IIO_VAL_INT;
  177. break;
  178. case IIO_PROXIMITY:
  179. *val = i2c_smbus_read_byte(cm36651->ps_client);
  180. if (*val < 0)
  181. return ret;
  182. if (!test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
  183. ret = i2c_smbus_write_byte_data(cm36651->ps_client,
  184. CM36651_PS_CONF1, CM36651_PS_DISABLE);
  185. if (ret < 0)
  186. return ret;
  187. }
  188. ret = IIO_VAL_INT;
  189. break;
  190. default:
  191. break;
  192. }
  193. return ret;
  194. }
  195. static irqreturn_t cm36651_irq_handler(int irq, void *data)
  196. {
  197. struct iio_dev *indio_dev = data;
  198. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  199. struct i2c_client *client = cm36651->client;
  200. int ev_dir, ret;
  201. u64 ev_code;
  202. /*
  203. * The PS INT pin is an active low signal that PS INT move logic low
  204. * when the object is detect. Once the MCU host received the PS INT
  205. * "LOW" signal, the Host needs to read the data at Alert Response
  206. * Address(ARA) to clear the PS INT signal. After clearing the PS
  207. * INT pin, the PS INT signal toggles from low to high.
  208. */
  209. ret = i2c_smbus_read_byte(cm36651->ara_client);
  210. if (ret < 0) {
  211. dev_err(&client->dev,
  212. "%s: Data read failed: %d\n", __func__, ret);
  213. return IRQ_HANDLED;
  214. }
  215. switch (ret) {
  216. case CM36651_CLOSE_PROXIMITY:
  217. ev_dir = IIO_EV_DIR_RISING;
  218. break;
  219. case CM36651_FAR_PROXIMITY:
  220. ev_dir = IIO_EV_DIR_FALLING;
  221. break;
  222. default:
  223. dev_err(&client->dev,
  224. "%s: Data read wrong: %d\n", __func__, ret);
  225. return IRQ_HANDLED;
  226. }
  227. ev_code = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
  228. CM36651_CMD_READ_RAW_PROXIMITY,
  229. IIO_EV_TYPE_THRESH, ev_dir);
  230. iio_push_event(indio_dev, ev_code, iio_get_time_ns());
  231. return IRQ_HANDLED;
  232. }
  233. static int cm36651_set_operation_mode(struct cm36651_data *cm36651, int cmd)
  234. {
  235. struct i2c_client *client = cm36651->client;
  236. struct i2c_client *ps_client = cm36651->ps_client;
  237. int ret = -EINVAL;
  238. switch (cmd) {
  239. case CM36651_CMD_READ_RAW_LIGHT:
  240. ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
  241. cm36651->cs_ctrl_regs[CM36651_CS_CONF1]);
  242. break;
  243. case CM36651_CMD_READ_RAW_PROXIMITY:
  244. if (test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags))
  245. return CM36651_PROXIMITY_EV_EN;
  246. ret = i2c_smbus_write_byte_data(ps_client, CM36651_PS_CONF1,
  247. cm36651->ps_ctrl_regs[CM36651_PS_CONF1]);
  248. break;
  249. case CM36651_CMD_PROX_EV_EN:
  250. if (test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
  251. dev_err(&client->dev,
  252. "Already proximity event enable state\n");
  253. return ret;
  254. }
  255. set_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
  256. ret = i2c_smbus_write_byte_data(ps_client,
  257. cm36651_ps_reg[CM36651_PS_CONF1],
  258. CM36651_PS_INT_EN | CM36651_PS_PERS2 | CM36651_PS_IT2);
  259. if (ret < 0) {
  260. dev_err(&client->dev, "Proximity enable event failed\n");
  261. return ret;
  262. }
  263. break;
  264. case CM36651_CMD_PROX_EV_DIS:
  265. if (!test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
  266. dev_err(&client->dev,
  267. "Already proximity event disable state\n");
  268. return ret;
  269. }
  270. clear_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
  271. ret = i2c_smbus_write_byte_data(ps_client,
  272. CM36651_PS_CONF1, CM36651_PS_DISABLE);
  273. break;
  274. }
  275. if (ret < 0)
  276. dev_err(&client->dev, "Write register failed\n");
  277. return ret;
  278. }
  279. static int cm36651_read_channel(struct cm36651_data *cm36651,
  280. struct iio_chan_spec const *chan, int *val)
  281. {
  282. struct i2c_client *client = cm36651->client;
  283. int cmd, ret;
  284. if (chan->type == IIO_LIGHT)
  285. cmd = CM36651_CMD_READ_RAW_LIGHT;
  286. else if (chan->type == IIO_PROXIMITY)
  287. cmd = CM36651_CMD_READ_RAW_PROXIMITY;
  288. else
  289. return -EINVAL;
  290. ret = cm36651_set_operation_mode(cm36651, cmd);
  291. if (ret < 0) {
  292. dev_err(&client->dev, "CM36651 set operation mode failed\n");
  293. return ret;
  294. }
  295. /* Delay for work after enable operation */
  296. msleep(50);
  297. ret = cm36651_read_output(cm36651, chan, val);
  298. if (ret < 0) {
  299. dev_err(&client->dev, "CM36651 read output failed\n");
  300. return ret;
  301. }
  302. return ret;
  303. }
  304. static int cm36651_read_int_time(struct cm36651_data *cm36651,
  305. struct iio_chan_spec const *chan, int *val2)
  306. {
  307. switch (chan->type) {
  308. case IIO_LIGHT:
  309. if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT1)
  310. *val2 = 80000;
  311. else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT2)
  312. *val2 = 160000;
  313. else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT3)
  314. *val2 = 320000;
  315. else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT4)
  316. *val2 = 640000;
  317. else
  318. return -EINVAL;
  319. break;
  320. case IIO_PROXIMITY:
  321. if (cm36651->ps_int_time == CM36651_PS_IT1)
  322. *val2 = 320;
  323. else if (cm36651->ps_int_time == CM36651_PS_IT2)
  324. *val2 = 420;
  325. else if (cm36651->ps_int_time == CM36651_PS_IT3)
  326. *val2 = 520;
  327. else if (cm36651->ps_int_time == CM36651_PS_IT4)
  328. *val2 = 640;
  329. else
  330. return -EINVAL;
  331. break;
  332. default:
  333. return -EINVAL;
  334. }
  335. return IIO_VAL_INT_PLUS_MICRO;
  336. }
  337. static int cm36651_write_int_time(struct cm36651_data *cm36651,
  338. struct iio_chan_spec const *chan, int val)
  339. {
  340. struct i2c_client *client = cm36651->client;
  341. struct i2c_client *ps_client = cm36651->ps_client;
  342. int int_time, ret;
  343. switch (chan->type) {
  344. case IIO_LIGHT:
  345. if (val == 80000)
  346. int_time = CM36651_CS_IT1;
  347. else if (val == 160000)
  348. int_time = CM36651_CS_IT2;
  349. else if (val == 320000)
  350. int_time = CM36651_CS_IT3;
  351. else if (val == 640000)
  352. int_time = CM36651_CS_IT4;
  353. else
  354. return -EINVAL;
  355. ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF3,
  356. int_time >> 2 * (chan->address));
  357. if (ret < 0) {
  358. dev_err(&client->dev, "CS integration time write failed\n");
  359. return ret;
  360. }
  361. cm36651->cs_int_time[chan->address] = int_time;
  362. break;
  363. case IIO_PROXIMITY:
  364. if (val == 320)
  365. int_time = CM36651_PS_IT1;
  366. else if (val == 420)
  367. int_time = CM36651_PS_IT2;
  368. else if (val == 520)
  369. int_time = CM36651_PS_IT3;
  370. else if (val == 640)
  371. int_time = CM36651_PS_IT4;
  372. else
  373. return -EINVAL;
  374. ret = i2c_smbus_write_byte_data(ps_client,
  375. CM36651_PS_CONF1, int_time);
  376. if (ret < 0) {
  377. dev_err(&client->dev, "PS integration time write failed\n");
  378. return ret;
  379. }
  380. cm36651->ps_int_time = int_time;
  381. break;
  382. default:
  383. return -EINVAL;
  384. }
  385. return ret;
  386. }
  387. static int cm36651_read_raw(struct iio_dev *indio_dev,
  388. struct iio_chan_spec const *chan,
  389. int *val, int *val2, long mask)
  390. {
  391. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  392. int ret;
  393. mutex_lock(&cm36651->lock);
  394. switch (mask) {
  395. case IIO_CHAN_INFO_RAW:
  396. ret = cm36651_read_channel(cm36651, chan, val);
  397. break;
  398. case IIO_CHAN_INFO_INT_TIME:
  399. *val = 0;
  400. ret = cm36651_read_int_time(cm36651, chan, val2);
  401. break;
  402. default:
  403. ret = -EINVAL;
  404. }
  405. mutex_unlock(&cm36651->lock);
  406. return ret;
  407. }
  408. static int cm36651_write_raw(struct iio_dev *indio_dev,
  409. struct iio_chan_spec const *chan,
  410. int val, int val2, long mask)
  411. {
  412. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  413. struct i2c_client *client = cm36651->client;
  414. int ret = -EINVAL;
  415. if (mask == IIO_CHAN_INFO_INT_TIME) {
  416. ret = cm36651_write_int_time(cm36651, chan, val2);
  417. if (ret < 0)
  418. dev_err(&client->dev, "Integration time write failed\n");
  419. }
  420. return ret;
  421. }
  422. static int cm36651_read_prox_thresh(struct iio_dev *indio_dev,
  423. const struct iio_chan_spec *chan,
  424. enum iio_event_type type,
  425. enum iio_event_direction dir,
  426. enum iio_event_info info,
  427. int *val, int *val2)
  428. {
  429. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  430. *val = cm36651->ps_ctrl_regs[CM36651_PS_THD];
  431. return 0;
  432. }
  433. static int cm36651_write_prox_thresh(struct iio_dev *indio_dev,
  434. const struct iio_chan_spec *chan,
  435. enum iio_event_type type,
  436. enum iio_event_direction dir,
  437. enum iio_event_info info,
  438. int val, int val2)
  439. {
  440. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  441. struct i2c_client *client = cm36651->client;
  442. int ret;
  443. if (val < 3 || val > 255)
  444. return -EINVAL;
  445. cm36651->ps_ctrl_regs[CM36651_PS_THD] = val;
  446. ret = i2c_smbus_write_byte_data(cm36651->ps_client, CM36651_PS_THD,
  447. cm36651->ps_ctrl_regs[CM36651_PS_THD]);
  448. if (ret < 0) {
  449. dev_err(&client->dev, "PS threshold write failed: %d\n", ret);
  450. return ret;
  451. }
  452. return 0;
  453. }
  454. static int cm36651_write_prox_event_config(struct iio_dev *indio_dev,
  455. const struct iio_chan_spec *chan,
  456. enum iio_event_type type,
  457. enum iio_event_direction dir,
  458. int state)
  459. {
  460. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  461. int cmd, ret = -EINVAL;
  462. mutex_lock(&cm36651->lock);
  463. cmd = state ? CM36651_CMD_PROX_EV_EN : CM36651_CMD_PROX_EV_DIS;
  464. ret = cm36651_set_operation_mode(cm36651, cmd);
  465. mutex_unlock(&cm36651->lock);
  466. return ret;
  467. }
  468. static int cm36651_read_prox_event_config(struct iio_dev *indio_dev,
  469. const struct iio_chan_spec *chan,
  470. enum iio_event_type type,
  471. enum iio_event_direction dir)
  472. {
  473. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  474. int event_en;
  475. mutex_lock(&cm36651->lock);
  476. event_en = test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
  477. mutex_unlock(&cm36651->lock);
  478. return event_en;
  479. }
  480. #define CM36651_LIGHT_CHANNEL(_color, _idx) { \
  481. .type = IIO_LIGHT, \
  482. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  483. BIT(IIO_CHAN_INFO_INT_TIME), \
  484. .address = _idx, \
  485. .modified = 1, \
  486. .channel2 = IIO_MOD_LIGHT_##_color, \
  487. } \
  488. static const struct iio_event_spec cm36651_event_spec[] = {
  489. {
  490. .type = IIO_EV_TYPE_THRESH,
  491. .dir = IIO_EV_DIR_EITHER,
  492. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  493. BIT(IIO_EV_INFO_ENABLE),
  494. }
  495. };
  496. static const struct iio_chan_spec cm36651_channels[] = {
  497. {
  498. .type = IIO_PROXIMITY,
  499. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  500. BIT(IIO_CHAN_INFO_INT_TIME),
  501. .event_spec = cm36651_event_spec,
  502. .num_event_specs = ARRAY_SIZE(cm36651_event_spec),
  503. },
  504. CM36651_LIGHT_CHANNEL(RED, CM36651_LIGHT_CHANNEL_IDX_RED),
  505. CM36651_LIGHT_CHANNEL(GREEN, CM36651_LIGHT_CHANNEL_IDX_GREEN),
  506. CM36651_LIGHT_CHANNEL(BLUE, CM36651_LIGHT_CHANNEL_IDX_BLUE),
  507. CM36651_LIGHT_CHANNEL(CLEAR, CM36651_LIGHT_CHANNEL_IDX_CLEAR),
  508. };
  509. static IIO_CONST_ATTR(in_illuminance_integration_time_available,
  510. CM36651_CS_INT_TIME_AVAIL);
  511. static IIO_CONST_ATTR(in_proximity_integration_time_available,
  512. CM36651_PS_INT_TIME_AVAIL);
  513. static struct attribute *cm36651_attributes[] = {
  514. &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
  515. &iio_const_attr_in_proximity_integration_time_available.dev_attr.attr,
  516. NULL,
  517. };
  518. static const struct attribute_group cm36651_attribute_group = {
  519. .attrs = cm36651_attributes
  520. };
  521. static const struct iio_info cm36651_info = {
  522. .driver_module = THIS_MODULE,
  523. .read_raw = &cm36651_read_raw,
  524. .write_raw = &cm36651_write_raw,
  525. .read_event_value = &cm36651_read_prox_thresh,
  526. .write_event_value = &cm36651_write_prox_thresh,
  527. .read_event_config = &cm36651_read_prox_event_config,
  528. .write_event_config = &cm36651_write_prox_event_config,
  529. .attrs = &cm36651_attribute_group,
  530. };
  531. static int cm36651_probe(struct i2c_client *client,
  532. const struct i2c_device_id *id)
  533. {
  534. struct cm36651_data *cm36651;
  535. struct iio_dev *indio_dev;
  536. int ret;
  537. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm36651));
  538. if (!indio_dev)
  539. return -ENOMEM;
  540. cm36651 = iio_priv(indio_dev);
  541. cm36651->vled_reg = devm_regulator_get(&client->dev, "vled");
  542. if (IS_ERR(cm36651->vled_reg)) {
  543. dev_err(&client->dev, "get regulator vled failed\n");
  544. return PTR_ERR(cm36651->vled_reg);
  545. }
  546. ret = regulator_enable(cm36651->vled_reg);
  547. if (ret) {
  548. dev_err(&client->dev, "enable regulator vled failed\n");
  549. return ret;
  550. }
  551. i2c_set_clientdata(client, indio_dev);
  552. cm36651->client = client;
  553. cm36651->ps_client = i2c_new_dummy(client->adapter,
  554. CM36651_I2C_ADDR_PS);
  555. if (!cm36651->ps_client) {
  556. dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
  557. ret = -ENODEV;
  558. goto error_disable_reg;
  559. }
  560. cm36651->ara_client = i2c_new_dummy(client->adapter, CM36651_ARA);
  561. if (!cm36651->ara_client) {
  562. dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
  563. ret = -ENODEV;
  564. goto error_i2c_unregister_ps;
  565. }
  566. mutex_init(&cm36651->lock);
  567. indio_dev->dev.parent = &client->dev;
  568. indio_dev->channels = cm36651_channels;
  569. indio_dev->num_channels = ARRAY_SIZE(cm36651_channels);
  570. indio_dev->info = &cm36651_info;
  571. indio_dev->name = id->name;
  572. indio_dev->modes = INDIO_DIRECT_MODE;
  573. ret = cm36651_setup_reg(cm36651);
  574. if (ret) {
  575. dev_err(&client->dev, "%s: register setup failed\n", __func__);
  576. goto error_i2c_unregister_ara;
  577. }
  578. ret = request_threaded_irq(client->irq, NULL, cm36651_irq_handler,
  579. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  580. "cm36651", indio_dev);
  581. if (ret) {
  582. dev_err(&client->dev, "%s: request irq failed\n", __func__);
  583. goto error_i2c_unregister_ara;
  584. }
  585. ret = iio_device_register(indio_dev);
  586. if (ret) {
  587. dev_err(&client->dev, "%s: regist device failed\n", __func__);
  588. goto error_free_irq;
  589. }
  590. return 0;
  591. error_free_irq:
  592. free_irq(client->irq, indio_dev);
  593. error_i2c_unregister_ara:
  594. i2c_unregister_device(cm36651->ara_client);
  595. error_i2c_unregister_ps:
  596. i2c_unregister_device(cm36651->ps_client);
  597. error_disable_reg:
  598. regulator_disable(cm36651->vled_reg);
  599. return ret;
  600. }
  601. static int cm36651_remove(struct i2c_client *client)
  602. {
  603. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  604. struct cm36651_data *cm36651 = iio_priv(indio_dev);
  605. iio_device_unregister(indio_dev);
  606. regulator_disable(cm36651->vled_reg);
  607. free_irq(client->irq, indio_dev);
  608. i2c_unregister_device(cm36651->ps_client);
  609. i2c_unregister_device(cm36651->ara_client);
  610. return 0;
  611. }
  612. static const struct i2c_device_id cm36651_id[] = {
  613. { "cm36651", 0 },
  614. { }
  615. };
  616. MODULE_DEVICE_TABLE(i2c, cm36651_id);
  617. static const struct of_device_id cm36651_of_match[] = {
  618. { .compatible = "capella,cm36651" },
  619. { }
  620. };
  621. MODULE_DEVICE_TABLE(of, cm36651_of_match);
  622. static struct i2c_driver cm36651_driver = {
  623. .driver = {
  624. .name = "cm36651",
  625. .of_match_table = cm36651_of_match,
  626. },
  627. .probe = cm36651_probe,
  628. .remove = cm36651_remove,
  629. .id_table = cm36651_id,
  630. };
  631. module_i2c_driver(cm36651_driver);
  632. MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
  633. MODULE_DESCRIPTION("CM36651 proximity/ambient light sensor driver");
  634. MODULE_LICENSE("GPL v2");