cm36653.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /* driver/sensor/cm36653.c
  2. * Copyright (c) 2011 SAMSUNG
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/i2c.h>
  23. #include <linux/errno.h>
  24. #include <linux/device.h>
  25. #include <linux/gpio.h>
  26. #include <linux/wakelock.h>
  27. #include <linux/input.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/slab.h>
  30. #include <linux/delay.h>
  31. #include <linux/fs.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/of_gpio.h>
  34. #include <linux/regulator/consumer.h>
  35. #include "sensors_core.h"
  36. /* For debugging */
  37. #undef CM36653_DEBUG
  38. #define VENDOR "CAPELLA"
  39. #define CHIP_ID "CM36653"
  40. #define MODULE_NAME_LIGHT "light_sensor"
  41. #define MODULE_NAME_PROX "proximity_sensor"
  42. #define I2C_M_WR 0 /* for i2c Write */
  43. #define I2c_M_RD 1 /* for i2c Read */
  44. #define REL_RED REL_HWHEEL
  45. #define REL_GREEN REL_DIAL
  46. #define REL_BLUE REL_WHEEL
  47. #define REL_WHITE REL_MISC
  48. /* register addresses */
  49. /* Ambient light sensor */
  50. #define REG_CS_CONF1 0x00
  51. #define REG_RED 0x08
  52. #define REG_GREEN 0x09
  53. #define REG_BLUE 0x0A
  54. #define REG_WHITE 0x0B
  55. /* Proximity sensor */
  56. #define REG_PS_CONF1 0x03
  57. #define REG_PS_THD 0x05
  58. #define REG_PS_CANC 0x06
  59. #define REG_PS_DATA 0x07
  60. #define ALS_REG_NUM 2
  61. #define PS_REG_NUM 3
  62. #define MSK_L(x) (x & 0xff)
  63. #define MSK_H(x) ((x & 0xff00) >> 8)
  64. /* Intelligent Cancelation*/
  65. #define CM36653_CANCELATION
  66. #ifdef CM36653_CANCELATION
  67. #define CANCELATION_FILE_PATH "/efs/prox_cal"
  68. #define CANCELATION_THRESHOLD 0x0806
  69. #endif
  70. #define PROX_READ_NUM 40
  71. /*lightsnesor log time 6SEC 200mec X 30*/
  72. #define LIGHT_LOG_TIME 30
  73. #define LIGHT_ADD_STARTTIME 300000000
  74. enum {
  75. LIGHT_ENABLED = BIT(0),
  76. PROXIMITY_ENABLED = BIT(1),
  77. };
  78. /* register settings */
  79. static u8 als_reg_setting[ALS_REG_NUM][2] = {
  80. {REG_CS_CONF1, 0x00}, /* enable */
  81. {REG_CS_CONF1, 0x01}, /* disable */
  82. };
  83. /* Change threshold value on the midas-sensor.c */
  84. enum {
  85. PS_CONF1 = 0,
  86. PS_THD,
  87. PS_CANCEL,
  88. };
  89. enum {
  90. REG_ADDR = 0,
  91. CMD,
  92. };
  93. static u16 ps_reg_init_setting[PS_REG_NUM][2] = {
  94. {REG_PS_CONF1, 0x430C}, /* REG_PS_CONF1 */
  95. {REG_PS_THD, 0x0A08}, /* REG_PS_THD */
  96. {REG_PS_CANC, 0x00}, /* REG_PS_CANC */
  97. };
  98. /* driver data */
  99. struct cm36653_data {
  100. struct i2c_client *i2c_client;
  101. struct wake_lock prx_wake_lock;
  102. struct input_dev *proximity_input_dev;
  103. struct input_dev *light_input_dev;
  104. struct mutex power_lock;
  105. struct mutex read_lock;
  106. struct hrtimer light_timer;
  107. struct hrtimer prox_timer;
  108. struct workqueue_struct *light_wq;
  109. struct workqueue_struct *prox_wq;
  110. struct work_struct work_light;
  111. struct work_struct work_prox;
  112. struct device *proximity_dev;
  113. struct device *light_dev;
  114. ktime_t light_poll_delay;
  115. ktime_t prox_poll_delay;
  116. int irq;
  117. int prox_int;
  118. int prox_led_on;
  119. bool is_led_on;
  120. u32 threshold;
  121. u8 power_state;
  122. int avg[3];
  123. u16 color[4];
  124. int count_log_time;
  125. #ifdef CM36653_CANCELATION
  126. u16 default_thresh;
  127. #endif
  128. };
  129. int cm36653_i2c_read_byte(struct cm36653_data *cm36653,
  130. u8 command, u8 *val)
  131. {
  132. int err = 0;
  133. int retry = 3;
  134. struct i2c_msg msg[1];
  135. struct i2c_client *client = cm36653->i2c_client;
  136. if ((client == NULL) || (!client->adapter))
  137. return -ENODEV;
  138. /* send slave address & command */
  139. msg->addr = client->addr;
  140. msg->flags = I2C_M_RD;
  141. msg->len = 1;
  142. msg->buf = val;
  143. while (retry--) {
  144. err = i2c_transfer(client->adapter, msg, 1);
  145. if (err >= 0)
  146. return err;
  147. }
  148. pr_err("[SENSOR] %s: i2c read failed at addr 0x%x: %d\n", __func__,
  149. client->addr, err);
  150. return err;
  151. }
  152. int cm36653_i2c_read_word(struct cm36653_data *cm36653, u8 command,
  153. u16 *val)
  154. {
  155. int err = 0;
  156. int retry = 3;
  157. struct i2c_client *client = cm36653->i2c_client;
  158. struct i2c_msg msg[2];
  159. unsigned char data[2] = {0,};
  160. u16 value = 0;
  161. if ((client == NULL) || (!client->adapter))
  162. return -ENODEV;
  163. while (retry--) {
  164. /* send slave address & command */
  165. msg[0].addr = client->addr;
  166. msg[0].flags = I2C_M_WR;
  167. msg[0].len = 1;
  168. msg[0].buf = &command;
  169. /* read word data */
  170. msg[1].addr = client->addr;
  171. msg[1].flags = I2C_M_RD;
  172. msg[1].len = 2;
  173. msg[1].buf = data;
  174. err = i2c_transfer(client->adapter, msg, 2);
  175. if (err >= 0) {
  176. value = (u16)data[1];
  177. *val = (value << 8) | (u16)data[0];
  178. return err;
  179. }
  180. }
  181. printk(KERN_ERR "[SENSOR] %s, i2c transfer error ret=%d\n", __func__, err);
  182. return err;
  183. }
  184. int cm36653_i2c_write_byte(struct cm36653_data *cm36653, u8 command,
  185. u8 val)
  186. {
  187. int err = 0;
  188. struct i2c_client *client = cm36653->i2c_client;
  189. struct i2c_msg msg[1];
  190. unsigned char data[2];
  191. int retry = 3;
  192. if ((client == NULL) || (!client->adapter))
  193. return -ENODEV;
  194. while (retry--) {
  195. data[0] = command;
  196. data[1] = val;
  197. /* send slave address & command */
  198. msg->addr = client->addr;
  199. msg->flags = I2C_M_WR;
  200. msg->len = 2;
  201. msg->buf = data;
  202. err = i2c_transfer(client->adapter, msg, 1);
  203. if (err >= 0)
  204. return 0;
  205. }
  206. pr_err("[SENSOR] %s, i2c transfer error(%d)\n", __func__, err);
  207. return err;
  208. }
  209. int cm36653_i2c_write_word(struct cm36653_data *cm36653, u8 command,
  210. u16 val)
  211. {
  212. int err = 0;
  213. struct i2c_client *client = cm36653->i2c_client;
  214. int retry = 3;
  215. if ((client == NULL) || (!client->adapter))
  216. return -ENODEV;
  217. while (retry--) {
  218. err = i2c_smbus_write_word_data(client, command, val);
  219. if (err >= 0)
  220. return 0;
  221. }
  222. pr_err("[SENSOR] %s, i2c transfer error(%d)\n", __func__, err);
  223. return err;
  224. }
  225. void prox_led_onoff(struct cm36653_data *cm36653, bool onoff)
  226. {
  227. if (onoff) {
  228. gpio_set_value(cm36653->prox_led_on, 1);
  229. msleep(20);
  230. } else
  231. gpio_set_value(cm36653->prox_led_on, 0);
  232. cm36653->is_led_on = onoff;
  233. usleep_range(1000, 1100);
  234. pr_info("[SENSOR] %s: onoff = %s, led_on_gpio = %s\n",
  235. __func__, onoff ? "on" : "off",
  236. gpio_get_value(cm36653->prox_led_on) ? "high" : "low");
  237. }
  238. static void cm36653_light_enable(struct cm36653_data *cm36653)
  239. {
  240. /* enable setting */
  241. cm36653_i2c_write_byte(cm36653, REG_CS_CONF1,
  242. als_reg_setting[0][1]);
  243. hrtimer_start(&cm36653->light_timer, cm36653->light_poll_delay,
  244. HRTIMER_MODE_REL);
  245. }
  246. static void cm36653_light_disable(struct cm36653_data *cm36653)
  247. {
  248. /* disable setting */
  249. cm36653_i2c_write_byte(cm36653, REG_CS_CONF1,
  250. als_reg_setting[1][1]);
  251. hrtimer_cancel(&cm36653->light_timer);
  252. cancel_work_sync(&cm36653->work_light);
  253. }
  254. /* sysfs */
  255. static ssize_t cm36653_poll_delay_show(struct device *dev,
  256. struct device_attribute *attr, char *buf)
  257. {
  258. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  259. return sprintf(buf, "%lld\n", ktime_to_ns(cm36653->light_poll_delay));
  260. }
  261. static ssize_t cm36653_poll_delay_store(struct device *dev,
  262. struct device_attribute *attr,
  263. const char *buf, size_t size)
  264. {
  265. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  266. int64_t new_delay;
  267. int err;
  268. err = strict_strtoll(buf, 10, &new_delay);
  269. if (err < 0)
  270. return err;
  271. mutex_lock(&cm36653->power_lock);
  272. if (new_delay != ktime_to_ns(cm36653->light_poll_delay)) {
  273. cm36653->light_poll_delay = ns_to_ktime(new_delay);
  274. if (cm36653->power_state & LIGHT_ENABLED) {
  275. cm36653_light_disable(cm36653);
  276. cm36653_light_enable(cm36653);
  277. }
  278. pr_info("[SENSOR] %s, poll_delay = %lld\n", __func__, new_delay);
  279. }
  280. mutex_unlock(&cm36653->power_lock);
  281. return size;
  282. }
  283. static ssize_t light_enable_store(struct device *dev,
  284. struct device_attribute *attr,
  285. const char *buf, size_t size)
  286. {
  287. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  288. bool new_value;
  289. if (sysfs_streq(buf, "1"))
  290. new_value = true;
  291. else if (sysfs_streq(buf, "0"))
  292. new_value = false;
  293. else {
  294. pr_err("[SENSOR] %s: invalid value %d\n", __func__, *buf);
  295. return -EINVAL;
  296. }
  297. mutex_lock(&cm36653->power_lock);
  298. pr_info("[SENSOR] %s,new_value=%d\n", __func__, new_value);
  299. if (new_value && !(cm36653->power_state & LIGHT_ENABLED)) {
  300. cm36653->power_state |= LIGHT_ENABLED;
  301. cm36653_light_enable(cm36653);
  302. } else if (!new_value && (cm36653->power_state & LIGHT_ENABLED)) {
  303. cm36653_light_disable(cm36653);
  304. cm36653->power_state &= ~LIGHT_ENABLED;
  305. }
  306. mutex_unlock(&cm36653->power_lock);
  307. return size;
  308. }
  309. static ssize_t light_enable_show(struct device *dev,
  310. struct device_attribute *attr, char *buf)
  311. {
  312. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  313. return sprintf(buf, "%d\n",
  314. (cm36653->power_state & LIGHT_ENABLED) ? 1 : 0);
  315. }
  316. #ifdef CM36653_CANCELATION
  317. static int proximity_open_cancelation(struct cm36653_data *data)
  318. {
  319. struct file *cancel_filp = NULL;
  320. int err = 0;
  321. mm_segment_t old_fs;
  322. old_fs = get_fs();
  323. set_fs(KERNEL_DS);
  324. cancel_filp = filp_open(CANCELATION_FILE_PATH, O_RDONLY, 0666);
  325. if (IS_ERR(cancel_filp)) {
  326. err = PTR_ERR(cancel_filp);
  327. if (err != -ENOENT)
  328. pr_err("[SENSOR] %s: Can't open cancelation file\n", __func__);
  329. set_fs(old_fs);
  330. return err;
  331. }
  332. err = cancel_filp->f_op->read(cancel_filp,
  333. (char *)&ps_reg_init_setting[PS_CANCEL][CMD],
  334. sizeof(u8), &cancel_filp->f_pos);
  335. if (err != sizeof(u8)) {
  336. pr_err("[SENSOR] %s: Can't read the cancel data from file\n", __func__);
  337. err = -EIO;
  338. }
  339. /*If there is an offset cal data. */
  340. if (ps_reg_init_setting[PS_CANCEL][CMD] != 0)
  341. ps_reg_init_setting[PS_THD][CMD] = CANCELATION_THRESHOLD;
  342. pr_info("[SENSOR] %s: proximity ps_data = %d, ps_thresh = 0x%x\n",
  343. __func__, ps_reg_init_setting[PS_CANCEL][CMD],
  344. ps_reg_init_setting[PS_THD][CMD]);
  345. filp_close(cancel_filp, current->files);
  346. set_fs(old_fs);
  347. return err;
  348. }
  349. static int proximity_store_cancelation(struct device *dev, bool do_calib)
  350. {
  351. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  352. struct file *cancel_filp = NULL;
  353. mm_segment_t old_fs;
  354. int err = 0;
  355. u16 ps_data = 0;
  356. if (do_calib) {
  357. mutex_lock(&cm36653->read_lock);
  358. cm36653_i2c_read_word(cm36653,
  359. REG_PS_DATA, &ps_data);
  360. ps_reg_init_setting[PS_CANCEL][CMD] = 0xff & ps_data;
  361. mutex_unlock(&cm36653->read_lock);
  362. ps_reg_init_setting[PS_THD][CMD] = CANCELATION_THRESHOLD;
  363. } else { /* reset */
  364. ps_reg_init_setting[PS_CANCEL][CMD] = 0;
  365. ps_reg_init_setting[PS_THD][CMD] = cm36653->default_thresh;
  366. }
  367. cm36653_i2c_write_word(cm36653, REG_PS_THD,
  368. ps_reg_init_setting[PS_THD][CMD]);
  369. cm36653_i2c_write_byte(cm36653, REG_PS_CANC,
  370. ps_reg_init_setting[PS_CANCEL][CMD]);
  371. pr_info("[SENSOR] %s: prox_cal = 0x%x, prox_thresh = 0x%x\n",
  372. __func__, ps_reg_init_setting[PS_CANCEL][CMD],
  373. ps_reg_init_setting[PS_THD][CMD]);
  374. old_fs = get_fs();
  375. set_fs(KERNEL_DS);
  376. cancel_filp = filp_open(CANCELATION_FILE_PATH,
  377. O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, 0666);
  378. if (IS_ERR(cancel_filp)) {
  379. pr_err("[SENSOR] %s: Can't open cancelation file\n", __func__);
  380. set_fs(old_fs);
  381. err = PTR_ERR(cancel_filp);
  382. return err;
  383. }
  384. err = cancel_filp->f_op->write(cancel_filp,
  385. (char *)&ps_reg_init_setting[PS_CANCEL][CMD],
  386. sizeof(u8), &cancel_filp->f_pos);
  387. if (err != sizeof(u8)) {
  388. pr_err("[SENSOR] %s: Can't write the cancel data to file\n", __func__);
  389. err = -EIO;
  390. }
  391. filp_close(cancel_filp, current->files);
  392. set_fs(old_fs);
  393. if (!do_calib) /* delay for clearing */
  394. msleep(150);
  395. return err;
  396. }
  397. static ssize_t proximity_cancel_store(struct device *dev,
  398. struct device_attribute *attr,
  399. const char *buf, size_t size)
  400. {
  401. bool do_calib;
  402. int err;
  403. if (sysfs_streq(buf, "1")) /* calibrate cancelation value */
  404. do_calib = true;
  405. else if (sysfs_streq(buf, "0")) /* reset cancelation value */
  406. do_calib = false;
  407. else {
  408. pr_debug("[SENSOR] %s: invalid value %d\n", __func__, *buf);
  409. return -EINVAL;
  410. }
  411. err = proximity_store_cancelation(dev, do_calib);
  412. if (err < 0) {
  413. pr_err("[SENSOR] %s: proximity_store_cancelation() failed\n", __func__);
  414. return err;
  415. }
  416. return size;
  417. }
  418. static ssize_t proximity_cancel_show(struct device *dev,
  419. struct device_attribute *attr, char *buf)
  420. {
  421. return sprintf(buf, "%d,%d\n", ps_reg_init_setting[PS_CANCEL][CMD],
  422. ps_reg_init_setting[PS_THD][CMD] >> 8 & 0xff);
  423. }
  424. #endif
  425. static ssize_t proximity_enable_store(struct device *dev,
  426. struct device_attribute *attr,
  427. const char *buf, size_t size)
  428. {
  429. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  430. bool new_value;
  431. if (sysfs_streq(buf, "1"))
  432. new_value = true;
  433. else if (sysfs_streq(buf, "0"))
  434. new_value = false;
  435. else {
  436. pr_err("[SENSOR] %s: invalid value %d\n", __func__, *buf);
  437. return -EINVAL;
  438. }
  439. mutex_lock(&cm36653->power_lock);
  440. pr_info("[SENSOR] %s, new_value = %d, threshold = %d\n", __func__, new_value,
  441. ps_reg_init_setting[PS_THD][CMD] >> 8 & 0xff);
  442. if (new_value && !(cm36653->power_state & PROXIMITY_ENABLED)) {
  443. u8 val = 1;
  444. int i, err = 0;
  445. cm36653->power_state |= PROXIMITY_ENABLED;
  446. if (cm36653->prox_led_on)
  447. prox_led_onoff(cm36653, true);
  448. #ifdef CM36653_CANCELATION
  449. /* open cancelation data */
  450. err = proximity_open_cancelation(cm36653);
  451. if (err < 0 && err != -ENOENT)
  452. pr_err("[SENSOR] %s: proximity_open_cancelation() failed\n",
  453. __func__);
  454. #endif
  455. /* enable settings */
  456. for (i = 0; i < PS_REG_NUM; i++) {
  457. cm36653_i2c_write_word(cm36653,
  458. ps_reg_init_setting[i][REG_ADDR],
  459. ps_reg_init_setting[i][CMD]);
  460. }
  461. val = gpio_get_value(cm36653->prox_int);
  462. /* 0 is close, 1 is far */
  463. input_report_abs(cm36653->proximity_input_dev,
  464. ABS_DISTANCE, val);
  465. input_sync(cm36653->proximity_input_dev);
  466. enable_irq(cm36653->irq);
  467. enable_irq_wake(cm36653->irq);
  468. } else if (!new_value && (cm36653->power_state & PROXIMITY_ENABLED)) {
  469. cm36653->power_state &= ~PROXIMITY_ENABLED;
  470. disable_irq_wake(cm36653->irq);
  471. disable_irq(cm36653->irq);
  472. /* disable settings */
  473. cm36653_i2c_write_word(cm36653, REG_PS_CONF1, 0x0001);
  474. if (cm36653->prox_led_on)
  475. prox_led_onoff(cm36653, false);
  476. }
  477. mutex_unlock(&cm36653->power_lock);
  478. return size;
  479. }
  480. static ssize_t proximity_enable_show(struct device *dev,
  481. struct device_attribute *attr, char *buf)
  482. {
  483. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  484. return sprintf(buf, "%d\n",
  485. (cm36653->power_state & PROXIMITY_ENABLED) ? 1 : 0);
  486. }
  487. static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
  488. cm36653_poll_delay_show, cm36653_poll_delay_store);
  489. static struct device_attribute dev_attr_light_enable =
  490. __ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  491. light_enable_show, light_enable_store);
  492. static struct device_attribute dev_attr_proximity_enable =
  493. __ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  494. proximity_enable_show, proximity_enable_store);
  495. static struct attribute *light_sysfs_attrs[] = {
  496. &dev_attr_light_enable.attr,
  497. &dev_attr_poll_delay.attr,
  498. NULL
  499. };
  500. static struct attribute_group light_attribute_group = {
  501. .attrs = light_sysfs_attrs,
  502. };
  503. static struct attribute *proximity_sysfs_attrs[] = {
  504. &dev_attr_proximity_enable.attr,
  505. NULL
  506. };
  507. static struct attribute_group proximity_attribute_group = {
  508. .attrs = proximity_sysfs_attrs,
  509. };
  510. /* sysfs for vendor & name */
  511. static ssize_t cm36653_vendor_show(struct device *dev,
  512. struct device_attribute *attr, char *buf)
  513. {
  514. return sprintf(buf, "%s\n", VENDOR);
  515. }
  516. static ssize_t cm36653_name_show(struct device *dev,
  517. struct device_attribute *attr, char *buf)
  518. {
  519. return sprintf(buf, "%s\n", CHIP_ID);
  520. }
  521. static struct device_attribute dev_attr_prox_sensor_vendor =
  522. __ATTR(vendor, S_IRUSR | S_IRGRP, cm36653_vendor_show, NULL);
  523. static struct device_attribute dev_attr_light_sensor_vendor =
  524. __ATTR(vendor, S_IRUSR | S_IRGRP, cm36653_vendor_show, NULL);
  525. static struct device_attribute dev_attr_prox_sensor_name =
  526. __ATTR(name, S_IRUSR | S_IRGRP, cm36653_name_show, NULL);
  527. static struct device_attribute dev_attr_light_sensor_name =
  528. __ATTR(name, S_IRUSR | S_IRGRP, cm36653_name_show, NULL);
  529. /* proximity sysfs */
  530. static ssize_t proximity_avg_show(struct device *dev,
  531. struct device_attribute *attr, char *buf)
  532. {
  533. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  534. return sprintf(buf, "%d,%d,%d\n", cm36653->avg[0],
  535. cm36653->avg[1], cm36653->avg[2]);
  536. }
  537. static ssize_t proximity_avg_store(struct device *dev,
  538. struct device_attribute *attr, const char *buf, size_t size)
  539. {
  540. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  541. bool new_value = false;
  542. if (sysfs_streq(buf, "1"))
  543. new_value = true;
  544. else if (sysfs_streq(buf, "0"))
  545. new_value = false;
  546. else {
  547. pr_err("[SENSOR] %s, invalid value %d\n", __func__, *buf);
  548. return -EINVAL;
  549. }
  550. pr_info("[SENSOR] %s, average enable = %d\n", __func__, new_value);
  551. mutex_lock(&cm36653->power_lock);
  552. if (new_value) {
  553. if (!(cm36653->power_state & PROXIMITY_ENABLED)) {
  554. if (cm36653->prox_led_on)
  555. prox_led_onoff(cm36653, true);
  556. cm36653_i2c_write_word(cm36653, REG_PS_CONF1,
  557. ps_reg_init_setting[PS_CONF1][CMD]);
  558. }
  559. hrtimer_start(&cm36653->prox_timer, cm36653->prox_poll_delay,
  560. HRTIMER_MODE_REL);
  561. } else if (!new_value) {
  562. hrtimer_cancel(&cm36653->prox_timer);
  563. cancel_work_sync(&cm36653->work_prox);
  564. if (!(cm36653->power_state & PROXIMITY_ENABLED)) {
  565. cm36653_i2c_write_word(cm36653, REG_PS_CONF1,
  566. 0x0001);
  567. if (cm36653->prox_led_on)
  568. prox_led_onoff(cm36653, false);
  569. }
  570. }
  571. mutex_unlock(&cm36653->power_lock);
  572. return size;
  573. }
  574. static ssize_t proximity_state_show(struct device *dev,
  575. struct device_attribute *attr, char *buf)
  576. {
  577. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  578. u16 ps_data;
  579. mutex_lock(&cm36653->power_lock);
  580. if (!(cm36653->power_state & PROXIMITY_ENABLED)) {
  581. if (cm36653->prox_led_on)
  582. prox_led_onoff(cm36653, true);
  583. cm36653_i2c_write_word(cm36653, REG_PS_CONF1,
  584. ps_reg_init_setting[PS_CONF1][CMD]);
  585. }
  586. mutex_lock(&cm36653->read_lock);
  587. cm36653_i2c_read_word(cm36653, REG_PS_DATA,
  588. &ps_data);
  589. mutex_unlock(&cm36653->read_lock);
  590. if (!(cm36653->power_state & PROXIMITY_ENABLED)) {
  591. cm36653_i2c_write_word(cm36653, REG_PS_CONF1,
  592. 0x0001);
  593. if (cm36653->prox_led_on)
  594. prox_led_onoff(cm36653, false);
  595. }
  596. mutex_unlock(&cm36653->power_lock);
  597. return sprintf(buf, "%d\n", (u8)(0xff & ps_data));
  598. }
  599. static ssize_t proximity_thresh_show(struct device *dev,
  600. struct device_attribute *attr, char *buf)
  601. {
  602. return sprintf(buf, "prox_threshold = %d\n",
  603. ps_reg_init_setting[PS_THD][CMD] >> 8 & 0xff);
  604. }
  605. static ssize_t proximity_thresh_store(struct device *dev,
  606. struct device_attribute *attr, const char *buf, size_t size)
  607. {
  608. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  609. u8 thresh_value = ps_reg_init_setting[PS_THD][CMD] >> 8 & 0xff;
  610. int err;
  611. err = kstrtou8(buf, 10, &thresh_value);
  612. if (err < 0)
  613. pr_err("[SENSOR] %s, kstrtou8 failed.", __func__);
  614. if (thresh_value > 2) {
  615. ps_reg_init_setting[PS_THD][CMD] =
  616. (u16)(thresh_value << 8 | (thresh_value - 2));
  617. err = cm36653_i2c_write_word(cm36653,
  618. REG_PS_THD, ps_reg_init_setting[PS_THD][CMD]);
  619. if (err < 0)
  620. pr_err("[SENSOR] %s: cm36653_ps_reg is failed. %d\n", __func__,
  621. err);
  622. pr_info("[SENSOR] %s, new threshold = 0x%x\n",
  623. __func__, thresh_value);
  624. msleep(150);
  625. } else
  626. pr_err("[SENSOR] %s, wrong threshold value(0x%x)!!\n",
  627. __func__, thresh_value);
  628. return size;
  629. }
  630. #ifdef CM36653_CANCELATION
  631. static DEVICE_ATTR(prox_cal, S_IRUGO | S_IWUSR | S_IWGRP,
  632. proximity_cancel_show, proximity_cancel_store);
  633. #endif
  634. static DEVICE_ATTR(prox_avg, S_IRUGO | S_IWUSR | S_IWGRP,
  635. proximity_avg_show, proximity_avg_store);
  636. static DEVICE_ATTR(state, S_IRUGO,
  637. proximity_state_show, NULL);
  638. static DEVICE_ATTR(prox_thresh, S_IRUGO | S_IWUSR | S_IWGRP,
  639. proximity_thresh_show, proximity_thresh_store);
  640. static struct device_attribute dev_attr_prox_raw =
  641. __ATTR(raw_data, S_IRUGO, proximity_state_show, NULL);
  642. static struct device_attribute *prox_sensor_attrs[] = {
  643. &dev_attr_prox_sensor_vendor,
  644. &dev_attr_prox_sensor_name,
  645. &dev_attr_prox_cal,
  646. &dev_attr_prox_avg,
  647. &dev_attr_state,
  648. &dev_attr_prox_thresh,
  649. &dev_attr_prox_raw,
  650. NULL,
  651. };
  652. /* light sysfs */
  653. static ssize_t light_lux_show(struct device *dev,
  654. struct device_attribute *attr, char *buf)
  655. {
  656. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  657. return sprintf(buf, "%u,%u,%u,%u\n",
  658. cm36653->color[0]+1, cm36653->color[1]+1,
  659. cm36653->color[2]+1, cm36653->color[3]+1);
  660. }
  661. static ssize_t light_data_show(struct device *dev,
  662. struct device_attribute *attr, char *buf)
  663. {
  664. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  665. return sprintf(buf, "%u,%u,%u,%u\n",
  666. cm36653->color[0]+1, cm36653->color[1]+1,
  667. cm36653->color[2]+1, cm36653->color[3]+1);
  668. }
  669. static DEVICE_ATTR(lux, S_IRUGO, light_lux_show, NULL);
  670. static struct device_attribute dev_attr_light_raw =
  671. __ATTR(raw_data, S_IRUGO, light_data_show, NULL);
  672. static struct device_attribute *light_sensor_attrs[] = {
  673. &dev_attr_light_sensor_vendor,
  674. &dev_attr_light_sensor_name,
  675. &dev_attr_lux,
  676. &dev_attr_light_raw,
  677. NULL,
  678. };
  679. /* interrupt happened due to transition/change of near/far proximity state */
  680. irqreturn_t cm36653_irq_thread_fn(int irq, void *data)
  681. {
  682. struct cm36653_data *cm36653 = data;
  683. u8 val = 1;
  684. #ifdef CM36653_DEBUG
  685. static int count;
  686. #endif
  687. u16 ps_data = 0;
  688. val = gpio_get_value(cm36653->prox_int);
  689. cm36653_i2c_read_word(cm36653, REG_PS_DATA, &ps_data);
  690. #ifdef CM36653_DEBUG
  691. pr_info("[SENSOR] %s: count = %d\n", __func__, count++);
  692. #endif
  693. if (cm36653->power_state & PROXIMITY_ENABLED) {
  694. /* 0 is close, 1 is far */
  695. input_report_abs(cm36653->proximity_input_dev, ABS_DISTANCE, val);
  696. input_sync(cm36653->proximity_input_dev);
  697. }
  698. wake_lock_timeout(&cm36653->prx_wake_lock, 3 * HZ);
  699. pr_info("[SENSOR] %s: val = %d, ps_data = %d (close:0, far:1)\n",
  700. __func__, val, (u8)(0xff & ps_data));
  701. return IRQ_HANDLED;
  702. }
  703. static int cm36653_setup_reg(struct cm36653_data *cm36653)
  704. {
  705. int err = 0, i = 0;
  706. u16 tmp = 0;
  707. /* ALS initialization */
  708. err = cm36653_i2c_write_byte(cm36653,
  709. als_reg_setting[0][0],
  710. als_reg_setting[0][1]);
  711. if (err < 0) {
  712. pr_err("[SENSOR] %s: cm36653_als_reg is failed. %d\n", __func__,
  713. err);
  714. return err;
  715. }
  716. /* PS initialization */
  717. for (i = 0; i < PS_REG_NUM; i++) {
  718. err = cm36653_i2c_write_word(cm36653,
  719. ps_reg_init_setting[i][REG_ADDR],
  720. ps_reg_init_setting[i][CMD]);
  721. if (err < 0) {
  722. pr_err("[SENSOR] %s: cm36653_ps_reg is failed. %d\n", __func__,
  723. err);
  724. return err;
  725. }
  726. }
  727. /* printing the inital proximity value with no contact */
  728. msleep(50);
  729. mutex_lock(&cm36653->read_lock);
  730. err = cm36653_i2c_read_word(cm36653, REG_PS_DATA, &tmp);
  731. mutex_unlock(&cm36653->read_lock);
  732. if (err < 0) {
  733. pr_err("[SENSOR] %s: read ps_data failed\n", __func__);
  734. err = -EIO;
  735. }
  736. pr_err("[SENSOR] %s: initial proximity value = %d\n",
  737. __func__, (u8)(0xff & tmp));
  738. /* turn off */
  739. cm36653_i2c_write_byte(cm36653, REG_CS_CONF1, 0x01);
  740. cm36653_i2c_write_word(cm36653, REG_PS_CONF1, 0x0001);
  741. pr_info("[SENSOR] %s is success.", __func__);
  742. return err;
  743. }
  744. static int cm36653_setup_gpio(struct cm36653_data *cm36653)
  745. {
  746. int rc = -EIO;
  747. rc = gpio_request(cm36653->prox_led_on, "gpio_prox_led_on");
  748. if (rc < 0) {
  749. pr_err("[SENSOR] %s: gpio %d request failed (%d)\n",
  750. __func__, cm36653->prox_led_on, rc);
  751. return rc;
  752. }
  753. gpio_direction_output(cm36653->prox_led_on, 0);
  754. rc = gpio_request(cm36653->prox_int, "gpio_proximity_out");
  755. if (rc < 0) {
  756. pr_err("[SENSOR] %s: gpio %d request failed (%d)\n",
  757. __func__, cm36653->prox_int, rc);
  758. return rc;
  759. }
  760. rc = gpio_direction_input(cm36653->prox_int);
  761. if (rc < 0) {
  762. pr_err("[SENSOR] %s: failed to set gpio %d as input (%d)\n",
  763. __func__, cm36653->prox_int, rc);
  764. goto err_gpio_direction_input;
  765. }
  766. cm36653->irq = gpio_to_irq(cm36653->prox_int);
  767. rc = request_threaded_irq(cm36653->irq, NULL,
  768. cm36653_irq_thread_fn,
  769. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  770. "proximity_int", cm36653);
  771. if (rc < 0) {
  772. pr_err("[SENSOR] %s: request_irq(%d) failed for gpio %d (%d)\n",
  773. __func__, cm36653->irq, cm36653->prox_int, rc);
  774. goto err_request_irq;
  775. }
  776. /* start with interrupts disabled */
  777. disable_irq(cm36653->irq);
  778. pr_err("[SENSOR] %s, success\n", __func__);
  779. goto done;
  780. err_request_irq:
  781. err_gpio_direction_input:
  782. gpio_free(cm36653->prox_int);
  783. gpio_free(cm36653->prox_led_on);
  784. done:
  785. return rc;
  786. }
  787. /* This function is for light sensor. It operates every a few seconds.
  788. * It asks for work to be done on a thread because i2c needs a thread
  789. * context (slow and blocking) and then reschedules the timer to run again.
  790. */
  791. static enum hrtimer_restart cm36653_light_timer_func(struct hrtimer *timer)
  792. {
  793. struct cm36653_data *cm36653
  794. = container_of(timer, struct cm36653_data, light_timer);
  795. queue_work(cm36653->light_wq, &cm36653->work_light);
  796. hrtimer_forward_now(&cm36653->light_timer, cm36653->light_poll_delay);
  797. return HRTIMER_RESTART;
  798. }
  799. static void cm36653_work_func_light(struct work_struct *work)
  800. {
  801. struct cm36653_data *cm36653 = container_of(work, struct cm36653_data,
  802. work_light);
  803. mutex_lock(&cm36653->read_lock);
  804. cm36653_i2c_read_word(cm36653, REG_RED, &cm36653->color[0]);
  805. cm36653_i2c_read_word(cm36653, REG_GREEN, &cm36653->color[1]);
  806. cm36653_i2c_read_word(cm36653, REG_BLUE, &cm36653->color[2]);
  807. cm36653_i2c_read_word(cm36653, REG_WHITE, &cm36653->color[3]);
  808. mutex_unlock(&cm36653->read_lock);
  809. input_report_rel(cm36653->light_input_dev, REL_RED,
  810. cm36653->color[0]+1);
  811. input_report_rel(cm36653->light_input_dev, REL_GREEN,
  812. cm36653->color[1]+1);
  813. input_report_rel(cm36653->light_input_dev, REL_BLUE,
  814. cm36653->color[2]+1);
  815. input_report_rel(cm36653->light_input_dev, REL_WHITE,
  816. cm36653->color[3]+1);
  817. input_sync(cm36653->light_input_dev);
  818. if ((ktime_to_ms(cm36653->light_poll_delay) * (int64_t)cm36653->count_log_time)
  819. >= ((int64_t)LIGHT_LOG_TIME * MSEC_PER_SEC)) {
  820. pr_info("[SENSOR] %s, red = %u green = %u blue = %u white = %u\n",
  821. __func__, cm36653->color[0]+1, cm36653->color[1]+1,
  822. cm36653->color[2]+1, cm36653->color[3]+1);
  823. cm36653->count_log_time = 0;
  824. } else
  825. cm36653->count_log_time++;
  826. #ifdef CM36653_DEBUG
  827. pr_info("[SENSOR] %s, red = %u green = %u blue = %u white = %u\n",
  828. __func__, cm36653->color[0]+1, cm36653->color[1]+1,
  829. cm36653->color[2]+1, cm36653->color[3]+1);
  830. #endif
  831. }
  832. static void proxsensor_get_avg_val(struct cm36653_data *cm36653)
  833. {
  834. int min = 0, max = 0, avg = 0;
  835. int i;
  836. u16 ps_data = 0;
  837. for (i = 0; i < PROX_READ_NUM; i++) {
  838. msleep(40);
  839. cm36653_i2c_read_word(cm36653, REG_PS_DATA,
  840. &ps_data);
  841. avg += (u8)(0xff & ps_data);
  842. if (!i)
  843. min = ps_data;
  844. else if (ps_data < min)
  845. min = ps_data;
  846. if (ps_data > max)
  847. max = ps_data;
  848. }
  849. avg /= PROX_READ_NUM;
  850. cm36653->avg[0] = min;
  851. cm36653->avg[1] = avg;
  852. cm36653->avg[2] = max;
  853. }
  854. static void cm36653_work_func_prox(struct work_struct *work)
  855. {
  856. struct cm36653_data *cm36653 = container_of(work, struct cm36653_data,
  857. work_prox);
  858. proxsensor_get_avg_val(cm36653);
  859. }
  860. static enum hrtimer_restart cm36653_prox_timer_func(struct hrtimer *timer)
  861. {
  862. struct cm36653_data *cm36653
  863. = container_of(timer, struct cm36653_data, prox_timer);
  864. queue_work(cm36653->prox_wq, &cm36653->work_prox);
  865. hrtimer_forward_now(&cm36653->prox_timer, cm36653->prox_poll_delay);
  866. return HRTIMER_RESTART;
  867. }
  868. static int cm36653_parse_dt(struct cm36653_data *data, struct device *dev)
  869. {
  870. struct device_node *dNode = dev->of_node;
  871. enum of_gpio_flags flags;
  872. if (dNode == NULL)
  873. return -ENODEV;
  874. data->prox_int = of_get_named_gpio_flags(dNode,
  875. "cm36653-i2c,prox_int-gpio", 0, &flags);
  876. if (data->prox_int < 0) {
  877. pr_err("[SENSOR]: %s - get prox_int error\n", __func__);
  878. return -ENODEV;
  879. }
  880. data->prox_led_on = of_get_named_gpio_flags(dNode,
  881. "cm36653-i2c,prox_led_en-gpio", 0, &flags);
  882. if (data->prox_led_on < 0) {
  883. pr_err("[SENSOR]: %s - get prox_led_en error\n", __func__);
  884. return -ENODEV;
  885. }
  886. if (of_property_read_u32(dNode,
  887. "cm36653-i2c,threshold", &data->threshold) < 0)
  888. data->threshold = 0x0806;
  889. pr_info("[SENSOR] %s success, threshold = 0x%X\n",
  890. __func__, data->threshold);
  891. return 0;
  892. }
  893. static int cm36653_i2c_probe(struct i2c_client *client,
  894. const struct i2c_device_id *id)
  895. {
  896. int ret = -ENODEV;
  897. struct cm36653_data *cm36653 = NULL;
  898. pr_info("[SENSOR] %s: Probe Start!\n", __func__);
  899. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  900. pr_err("[SENSOR] %s: i2c functionality check failed!\n", __func__);
  901. return ret;
  902. }
  903. cm36653 = kzalloc(sizeof(struct cm36653_data), GFP_KERNEL);
  904. if (!cm36653) {
  905. pr_err
  906. ("[SENSOR] %s: failed to alloc memory for RGB sensor module data\n",
  907. __func__);
  908. return -ENOMEM;
  909. }
  910. cm36653->i2c_client = client;
  911. i2c_set_clientdata(client, cm36653);
  912. mutex_init(&cm36653->power_lock);
  913. mutex_init(&cm36653->read_lock);
  914. ret = cm36653_parse_dt(cm36653, &client->dev);
  915. if (ret < 0) {
  916. pr_err("[SENSOR] %s: - of_node error\n", __func__);
  917. ret = -ENODEV;
  918. goto err_of_node;
  919. }
  920. /* setup gpio */
  921. ret = cm36653_setup_gpio(cm36653);
  922. if (ret) {
  923. pr_err("[SENSOR] %s: could not setup gpio\n", __func__);
  924. goto err_setup_gpio;
  925. }
  926. prox_led_onoff(cm36653, true);
  927. /* wake lock init for proximity sensor */
  928. wake_lock_init(&cm36653->prx_wake_lock, WAKE_LOCK_SUSPEND,
  929. "prx_wake_lock");
  930. /* Check if the device is there or not. */
  931. ret = cm36653_i2c_write_byte(cm36653, REG_CS_CONF1, 0x01);
  932. if (ret < 0) {
  933. pr_err("[SENSOR] %s: cm36653 is not connected.(%d)\n", __func__, ret);
  934. goto err_setup_reg;
  935. }
  936. /* setup initial registers */
  937. ps_reg_init_setting[PS_THD][CMD] = cm36653->threshold;
  938. #ifdef CM36653_CANCELATION
  939. cm36653->default_thresh = ps_reg_init_setting[PS_THD][CMD];
  940. #endif
  941. ret = cm36653_setup_reg(cm36653);
  942. if (ret < 0) {
  943. pr_err("[SENSOR] %s: could not setup regs\n", __func__);
  944. goto err_setup_reg;
  945. }
  946. prox_led_onoff(cm36653, false);
  947. /* allocate proximity input_device */
  948. cm36653->proximity_input_dev = input_allocate_device();
  949. if (!cm36653->proximity_input_dev) {
  950. pr_err("[SENSOR] %s: could not allocate proximity input device\n",
  951. __func__);
  952. ret = -ENOMEM;
  953. goto err_input_allocate_device_proximity;
  954. }
  955. input_set_drvdata(cm36653->proximity_input_dev, cm36653);
  956. cm36653->proximity_input_dev->name = MODULE_NAME_PROX;
  957. input_set_capability(cm36653->proximity_input_dev, EV_ABS,
  958. ABS_DISTANCE);
  959. input_set_abs_params(cm36653->proximity_input_dev, ABS_DISTANCE, 0, 1,
  960. 0, 0);
  961. ret = input_register_device(cm36653->proximity_input_dev);
  962. if (ret < 0) {
  963. input_free_device(cm36653->proximity_input_dev);
  964. pr_err("[SENSOR] %s: could not register input device\n", __func__);
  965. goto err_input_register_device_proximity;
  966. }
  967. ret = sensors_create_symlink(&cm36653->proximity_input_dev->dev.kobj,
  968. cm36653->proximity_input_dev->name);
  969. if (ret < 0) {
  970. pr_err("[SENSOR] %s: create_symlink error\n", __func__);
  971. goto err_sensors_create_symlink_prox;
  972. }
  973. ret = sysfs_create_group(&cm36653->proximity_input_dev->dev.kobj,
  974. &proximity_attribute_group);
  975. if (ret) {
  976. pr_err("[SENSOR] %s: could not create sysfs group\n", __func__);
  977. goto err_sysfs_create_group_proximity;
  978. }
  979. /* For factory test mode, we use timer to get average proximity data. */
  980. /* prox_timer settings. we poll for light values using a timer. */
  981. hrtimer_init(&cm36653->prox_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  982. cm36653->prox_poll_delay = ns_to_ktime(2000 * NSEC_PER_MSEC);/*2 sec*/
  983. cm36653->prox_timer.function = cm36653_prox_timer_func;
  984. /* the timer just fires off a work queue request. we need a thread
  985. to read the i2c (can be slow and blocking). */
  986. cm36653->prox_wq = create_singlethread_workqueue("cm36653_prox_wq");
  987. if (!cm36653->prox_wq) {
  988. ret = -ENOMEM;
  989. pr_err("[SENSOR] %s: could not create prox workqueue\n", __func__);
  990. goto err_create_prox_workqueue;
  991. }
  992. /* this is the thread function we run on the work queue */
  993. INIT_WORK(&cm36653->work_prox, cm36653_work_func_prox);
  994. /* allocate lightsensor input_device */
  995. cm36653->light_input_dev = input_allocate_device();
  996. if (!cm36653->light_input_dev) {
  997. pr_err("[SENSOR] %s: could not allocate light input device\n", __func__);
  998. ret = -ENOMEM;
  999. goto err_input_allocate_device_light;
  1000. }
  1001. input_set_drvdata(cm36653->light_input_dev, cm36653);
  1002. cm36653->light_input_dev->name = MODULE_NAME_LIGHT;
  1003. input_set_capability(cm36653->light_input_dev, EV_REL, REL_RED);
  1004. input_set_capability(cm36653->light_input_dev, EV_REL, REL_GREEN);
  1005. input_set_capability(cm36653->light_input_dev, EV_REL, REL_BLUE);
  1006. input_set_capability(cm36653->light_input_dev, EV_REL, REL_WHITE);
  1007. ret = input_register_device(cm36653->light_input_dev);
  1008. if (ret < 0) {
  1009. input_free_device(cm36653->light_input_dev);
  1010. pr_err("%s: could not register input device\n", __func__);
  1011. goto err_input_register_device_light;
  1012. }
  1013. ret = sensors_create_symlink(&cm36653->light_input_dev->dev.kobj,
  1014. cm36653->light_input_dev->name);
  1015. if (ret < 0) {
  1016. pr_err("[SENSOR] %s: create_symlink error\n", __func__);
  1017. goto err_sensors_create_symlink_light;
  1018. }
  1019. ret = sysfs_create_group(&cm36653->light_input_dev->dev.kobj,
  1020. &light_attribute_group);
  1021. if (ret) {
  1022. pr_err("[SENSOR] %s: could not create sysfs group\n", __func__);
  1023. goto err_sysfs_create_group_light;
  1024. }
  1025. /* light_timer settings. we poll for light values using a timer. */
  1026. hrtimer_init(&cm36653->light_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1027. cm36653->light_poll_delay = ns_to_ktime(200 * NSEC_PER_MSEC);
  1028. cm36653->light_timer.function = cm36653_light_timer_func;
  1029. /* the timer just fires off a work queue request. we need a thread
  1030. to read the i2c (can be slow and blocking). */
  1031. cm36653->light_wq = create_singlethread_workqueue("cm36653_light_wq");
  1032. if (!cm36653->light_wq) {
  1033. ret = -ENOMEM;
  1034. pr_err("[SENSOR] %s: could not create light workqueue\n", __func__);
  1035. goto err_create_light_workqueue;
  1036. }
  1037. /* this is the thread function we run on the work queue */
  1038. INIT_WORK(&cm36653->work_light, cm36653_work_func_light);
  1039. /* set sysfs for proximity sensor */
  1040. ret = sensors_register(cm36653->proximity_dev,
  1041. cm36653, prox_sensor_attrs,
  1042. "proximity_sensor");
  1043. if (ret) {
  1044. pr_err("[SENSOR] %s: cound not register\
  1045. proximity sensor device(%d).\n",
  1046. __func__, ret);
  1047. goto prox_sensor_register_failed;
  1048. }
  1049. /* set sysfs for light sensor */
  1050. ret = sensors_register(cm36653->light_dev,
  1051. cm36653, light_sensor_attrs,
  1052. "light_sensor");
  1053. if (ret) {
  1054. pr_err("[SENSOR] %s: cound not register\
  1055. light sensor device(%d).\n",
  1056. __func__, ret);
  1057. goto light_sensor_register_failed;
  1058. }
  1059. pr_info("[SENSOR] %s is success.\n", __func__);
  1060. goto done;
  1061. /* error, unwind it all */
  1062. light_sensor_register_failed:
  1063. sensors_unregister(cm36653->proximity_dev, prox_sensor_attrs);
  1064. prox_sensor_register_failed:
  1065. destroy_workqueue(cm36653->light_wq);
  1066. err_create_light_workqueue:
  1067. sysfs_remove_group(&cm36653->light_input_dev->dev.kobj,
  1068. &light_attribute_group);
  1069. err_sysfs_create_group_light:
  1070. sensors_remove_symlink(&cm36653->light_input_dev->dev.kobj,
  1071. cm36653->light_input_dev->name);
  1072. err_sensors_create_symlink_light:
  1073. input_unregister_device(cm36653->light_input_dev);
  1074. err_input_register_device_light:
  1075. err_input_allocate_device_light:
  1076. destroy_workqueue(cm36653->prox_wq);
  1077. err_create_prox_workqueue:
  1078. sysfs_remove_group(&cm36653->proximity_input_dev->dev.kobj,
  1079. &proximity_attribute_group);
  1080. err_sysfs_create_group_proximity:
  1081. sensors_remove_symlink(&cm36653->proximity_input_dev->dev.kobj,
  1082. cm36653->proximity_input_dev->name);
  1083. err_sensors_create_symlink_prox:
  1084. input_unregister_device(cm36653->proximity_input_dev);
  1085. err_input_register_device_proximity:
  1086. err_input_allocate_device_proximity:
  1087. err_setup_reg:
  1088. wake_lock_destroy(&cm36653->prx_wake_lock);
  1089. prox_led_onoff(cm36653, false);
  1090. free_irq(cm36653->irq, cm36653);
  1091. gpio_free(cm36653->prox_int);
  1092. gpio_free(cm36653->prox_led_on);
  1093. err_setup_gpio:
  1094. err_of_node:
  1095. mutex_destroy(&cm36653->read_lock);
  1096. mutex_destroy(&cm36653->power_lock);
  1097. kfree(cm36653);
  1098. done:
  1099. return ret;
  1100. }
  1101. static int cm36653_i2c_remove(struct i2c_client *client)
  1102. {
  1103. struct cm36653_data *cm36653 = i2c_get_clientdata(client);
  1104. /* free irq */
  1105. if (cm36653->power_state & PROXIMITY_ENABLED) {
  1106. disable_irq_wake(cm36653->irq);
  1107. disable_irq(cm36653->irq);
  1108. }
  1109. free_irq(cm36653->irq, cm36653);
  1110. gpio_free(cm36653->prox_int);
  1111. /* device off */
  1112. if (cm36653->power_state & LIGHT_ENABLED)
  1113. cm36653_light_disable(cm36653);
  1114. if (cm36653->power_state & PROXIMITY_ENABLED) {
  1115. cm36653_i2c_write_byte(cm36653, REG_PS_CONF1, 0x01);
  1116. if (cm36653->prox_led_on)
  1117. prox_led_onoff(cm36653, false);
  1118. }
  1119. /* destroy workqueue */
  1120. destroy_workqueue(cm36653->light_wq);
  1121. destroy_workqueue(cm36653->prox_wq);
  1122. /* sysfs destroy */
  1123. sensors_unregister(cm36653->light_dev, light_sensor_attrs);
  1124. sensors_unregister(cm36653->proximity_dev, prox_sensor_attrs);
  1125. sensors_remove_symlink(&cm36653->light_input_dev->dev.kobj,
  1126. cm36653->light_input_dev->name);
  1127. sensors_remove_symlink(&cm36653->proximity_input_dev->dev.kobj,
  1128. cm36653->proximity_input_dev->name);
  1129. /* input device destroy */
  1130. sysfs_remove_group(&cm36653->light_input_dev->dev.kobj,
  1131. &light_attribute_group);
  1132. input_unregister_device(cm36653->light_input_dev);
  1133. sysfs_remove_group(&cm36653->proximity_input_dev->dev.kobj,
  1134. &proximity_attribute_group);
  1135. input_unregister_device(cm36653->proximity_input_dev);
  1136. /* lock destroy */
  1137. mutex_destroy(&cm36653->read_lock);
  1138. mutex_destroy(&cm36653->power_lock);
  1139. wake_lock_destroy(&cm36653->prx_wake_lock);
  1140. kfree(cm36653);
  1141. return 0;
  1142. }
  1143. static int cm36653_suspend(struct device *dev)
  1144. {
  1145. /* We disable power only if proximity is disabled. If proximity
  1146. is enabled, we leave power on because proximity is allowed
  1147. to wake up device. We remove power without changing
  1148. cm36653->power_state because we use that state in resume.
  1149. */
  1150. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  1151. if (cm36653->power_state & LIGHT_ENABLED)
  1152. cm36653_light_disable(cm36653);
  1153. return 0;
  1154. }
  1155. static int cm36653_resume(struct device *dev)
  1156. {
  1157. struct cm36653_data *cm36653 = dev_get_drvdata(dev);
  1158. if (cm36653->power_state & LIGHT_ENABLED)
  1159. cm36653_light_enable(cm36653);
  1160. return 0;
  1161. }
  1162. static struct of_device_id cm36653_match_table[] = {
  1163. { .compatible = "cm36653-i2c",},
  1164. {},
  1165. };
  1166. static const struct i2c_device_id cm36653_device_id[] = {
  1167. {"cm36653_match_table", 0},
  1168. {}
  1169. };
  1170. static const struct dev_pm_ops cm36653_pm_ops = {
  1171. .suspend = cm36653_suspend,
  1172. .resume = cm36653_resume
  1173. };
  1174. static struct i2c_driver cm36653_i2c_driver = {
  1175. .driver = {
  1176. .name = CHIP_ID,
  1177. .owner = THIS_MODULE,
  1178. .of_match_table = cm36653_match_table,
  1179. .pm = &cm36653_pm_ops
  1180. },
  1181. .probe = cm36653_i2c_probe,
  1182. .remove = cm36653_i2c_remove,
  1183. .id_table = cm36653_device_id,
  1184. };
  1185. static int __init cm36653_init(void)
  1186. {
  1187. return i2c_add_driver(&cm36653_i2c_driver);
  1188. }
  1189. static void __exit cm36653_exit(void)
  1190. {
  1191. i2c_del_driver(&cm36653_i2c_driver);
  1192. }
  1193. module_init(cm36653_init);
  1194. module_exit(cm36653_exit);
  1195. MODULE_AUTHOR("Samsung Electronics");
  1196. MODULE_DESCRIPTION("RGB Sensor device driver for cm36653");
  1197. MODULE_LICENSE("GPL");