cm36651.c 42 KB

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