cm36686.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. /* driver/sensor/cm36686.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/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/i2c.h>
  22. #include <linux/errno.h>
  23. #include <linux/device.h>
  24. #include <linux/gpio.h>
  25. #include <linux/wakelock.h>
  26. #include <linux/input.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. #include <linux/fs.h>
  31. #include <linux/module.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/of_gpio.h>
  34. #include <linux/regulator/consumer.h>
  35. #include <linux/sensor/cm36686.h>
  36. #include <linux/sensor/sensors_core.h>
  37. /* For debugging */
  38. #undef cm36686_DEBUG
  39. #define VENDOR "CAPELLA"
  40. #define CHIP_ID "CM36686"
  41. #define I2C_M_WR 0 /* for i2c Write */
  42. #define I2c_M_RD 1 /* for i2c Read */
  43. /* register addresses */
  44. /* Ambient light sensor */
  45. #define REG_CS_CONF1 0x00
  46. #define REG_ALS_DATA 0x09
  47. #define REG_WHITE_DATA 0x0A
  48. /* Proximity sensor */
  49. #define REG_PS_CONF1 0x03
  50. #define REG_PS_CONF3 0x04
  51. #define REG_PS_CANC 0x05
  52. #define REG_PS_THD_LOW 0x06
  53. #define REG_PS_THD_HIGH 0x07
  54. #define REG_PS_DATA 0x08
  55. #define ALS_REG_NUM 2
  56. #define PS_REG_NUM 5
  57. #define MSK_L(x) (x & 0xff)
  58. #define MSK_H(x) ((x & 0xff00) >> 8)
  59. /* Intelligent Cancelation*/
  60. #define CM36686_CANCELATION
  61. #ifdef CM36686_CANCELATION
  62. #define SUCCESS 1
  63. #define FAIL 0
  64. #define ERROR -1
  65. #define CANCELATION_FILE_PATH "/efs/prox_cal"
  66. #define CAL_SKIP_ADC 4
  67. #define CAL_FAIL_ADC 14
  68. #endif
  69. #define PROX_READ_NUM 40
  70. /* proximity sensor threshold */
  71. #define DEFUALT_HI_THD 0x0022
  72. #define DEFUALT_LOW_THD 0x001E
  73. #define CANCEL_HI_THD 0x0022
  74. #define CANCEL_LOW_THD 0x001E
  75. /*lightsnesor log time 6SEC 200mec X 30*/
  76. #define LIGHT_LOG_TIME 30
  77. #define LIGHT_ADD_STARTTIME 300000000
  78. enum {
  79. LIGHT_ENABLED = BIT(0),
  80. PROXIMITY_ENABLED = BIT(1),
  81. };
  82. /* register settings */
  83. static u16 als_reg_setting[ALS_REG_NUM][2] = {
  84. {REG_CS_CONF1, 0x0000}, /* enable */
  85. {REG_CS_CONF1, 0x0001}, /* disable */
  86. };
  87. /* Change threshold value on the midas-sensor.c */
  88. enum {
  89. PS_CONF1 = 0,
  90. PS_CONF3,
  91. PS_THD_LOW,
  92. PS_THD_HIGH,
  93. PS_CANCEL,
  94. };
  95. enum {
  96. REG_ADDR = 0,
  97. CMD,
  98. };
  99. static u16 ps_reg_init_setting[PS_REG_NUM][2] = {
  100. {REG_PS_CONF1, 0x0300}, /* REG_PS_CONF1 */
  101. {REG_PS_CONF3, 0x4200}, /* REG_PS_CONF3 */
  102. {REG_PS_THD_LOW, DEFUALT_LOW_THD}, /* REG_PS_THD_LOW */
  103. {REG_PS_THD_HIGH, DEFUALT_HI_THD}, /* REG_PS_THD_HIGH */
  104. {REG_PS_CANC, 0x0000}, /* REG_PS_CANC */
  105. };
  106. /* driver data */
  107. struct cm36686_data {
  108. struct i2c_client *i2c_client;
  109. struct wake_lock prx_wake_lock;
  110. struct input_dev *proximity_input_dev;
  111. struct input_dev *light_input_dev;
  112. struct cm36686_platform_data *pdata;
  113. struct mutex power_lock;
  114. struct mutex read_lock;
  115. struct hrtimer light_timer;
  116. struct hrtimer prox_timer;
  117. struct workqueue_struct *light_wq;
  118. struct workqueue_struct *prox_wq;
  119. struct work_struct work_light;
  120. struct work_struct work_prox;
  121. struct device *proximity_dev;
  122. struct device *light_dev;
  123. ktime_t light_poll_delay;
  124. ktime_t prox_poll_delay;
  125. int irq;
  126. u8 power_state;
  127. int avg[3];
  128. u16 als_data;
  129. u16 white_data;
  130. int count_log_time;
  131. unsigned int uProxCalResult;
  132. };
  133. int cm36686_i2c_read_word(struct cm36686_data *cm36686, u8 command, u16 *val)
  134. {
  135. int err = 0;
  136. int retry = 3;
  137. struct i2c_client *client = cm36686->i2c_client;
  138. struct i2c_msg msg[2];
  139. unsigned char data[2] = {0,};
  140. u16 value = 0;
  141. if ((client == NULL) || (!client->adapter))
  142. return -ENODEV;
  143. while (retry--) {
  144. /* send slave address & command */
  145. msg[0].addr = client->addr;
  146. msg[0].flags = I2C_M_WR;
  147. msg[0].len = 1;
  148. msg[0].buf = &command;
  149. /* read word data */
  150. msg[1].addr = client->addr;
  151. msg[1].flags = I2C_M_RD;
  152. msg[1].len = 2;
  153. msg[1].buf = data;
  154. err = i2c_transfer(client->adapter, msg, 2);
  155. if (err >= 0) {
  156. value = (u16)data[1];
  157. *val = (value << 8) | (u16)data[0];
  158. return err;
  159. }
  160. }
  161. pr_err("%s, i2c transfer error ret=%d\n", __func__, err);
  162. return err;
  163. }
  164. int cm36686_i2c_write_word(struct cm36686_data *cm36686, u8 command,
  165. u16 val)
  166. {
  167. int err = 0;
  168. struct i2c_client *client = cm36686->i2c_client;
  169. int retry = 3;
  170. if ((client == NULL) || (!client->adapter))
  171. return -ENODEV;
  172. while (retry--) {
  173. err = i2c_smbus_write_word_data(client, command, val);
  174. if (err >= 0)
  175. return 0;
  176. }
  177. pr_err("%s, i2c transfer error(%d)\n", __func__, err);
  178. return err;
  179. }
  180. static void cm36686_light_enable(struct cm36686_data *cm36686)
  181. {
  182. /* enable setting */
  183. cm36686_i2c_write_word(cm36686, REG_CS_CONF1,
  184. als_reg_setting[0][1]);
  185. hrtimer_start(&cm36686->light_timer, ns_to_ktime(200 * NSEC_PER_MSEC),
  186. HRTIMER_MODE_REL);
  187. }
  188. static void cm36686_light_disable(struct cm36686_data *cm36686)
  189. {
  190. /* disable setting */
  191. cm36686_i2c_write_word(cm36686, REG_CS_CONF1,
  192. als_reg_setting[1][1]);
  193. hrtimer_cancel(&cm36686->light_timer);
  194. cancel_work_sync(&cm36686->work_light);
  195. }
  196. /* sysfs */
  197. static ssize_t cm36686_poll_delay_show(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  201. return snprintf(buf, PAGE_SIZE, "%lld\n",
  202. ktime_to_ns(cm36686->light_poll_delay));
  203. }
  204. static ssize_t cm36686_poll_delay_store(struct device *dev,
  205. struct device_attribute *attr, const char *buf, size_t size)
  206. {
  207. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  208. int64_t new_delay;
  209. int err;
  210. err = kstrtoll(buf, 10, &new_delay);
  211. if (err < 0)
  212. return err;
  213. mutex_lock(&cm36686->power_lock);
  214. if (new_delay != ktime_to_ns(cm36686->light_poll_delay)) {
  215. cm36686->light_poll_delay = ns_to_ktime(new_delay);
  216. if (cm36686->power_state & LIGHT_ENABLED) {
  217. cm36686_light_disable(cm36686);
  218. cm36686_light_enable(cm36686);
  219. }
  220. pr_info("[SENSOR] %s, poll_delay = %lld\n",
  221. __func__, new_delay);
  222. }
  223. mutex_unlock(&cm36686->power_lock);
  224. return size;
  225. }
  226. static ssize_t light_enable_store(struct device *dev,
  227. struct device_attribute *attr, const char *buf, size_t size)
  228. {
  229. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  230. bool new_value;
  231. if (sysfs_streq(buf, "1"))
  232. new_value = true;
  233. else if (sysfs_streq(buf, "0"))
  234. new_value = false;
  235. else {
  236. pr_err("[SENSOR] %s: invalid value %d\n", __func__, *buf);
  237. return -EINVAL;
  238. }
  239. mutex_lock(&cm36686->power_lock);
  240. pr_info("[SENSOR] %s,new_value=%d\n", __func__, new_value);
  241. if (new_value && !(cm36686->power_state & LIGHT_ENABLED)) {
  242. cm36686->power_state |= LIGHT_ENABLED;
  243. cm36686_light_enable(cm36686);
  244. } else if (!new_value && (cm36686->power_state & LIGHT_ENABLED)) {
  245. cm36686_light_disable(cm36686);
  246. cm36686->power_state &= ~LIGHT_ENABLED;
  247. }
  248. mutex_unlock(&cm36686->power_lock);
  249. return size;
  250. }
  251. static ssize_t light_enable_show(struct device *dev,
  252. struct device_attribute *attr, char *buf)
  253. {
  254. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  255. return snprintf(buf, PAGE_SIZE, "%d\n",
  256. (cm36686->power_state & LIGHT_ENABLED) ? 1 : 0);
  257. }
  258. #ifdef CM36686_CANCELATION
  259. void set_default_proximity_threshold(struct cm36686_data *data)
  260. {
  261. pr_info("%s: called\n", __func__);
  262. if (data->pdata->default_hi_thd) {
  263. ps_reg_init_setting[PS_THD_HIGH][CMD] =
  264. data->pdata->default_hi_thd;
  265. } else
  266. ps_reg_init_setting[PS_THD_HIGH][CMD] = DEFUALT_HI_THD;
  267. if (data->pdata->default_low_thd) {
  268. ps_reg_init_setting[PS_THD_LOW][CMD] =
  269. data->pdata->default_low_thd;
  270. } else
  271. ps_reg_init_setting[PS_THD_LOW][CMD] = DEFUALT_LOW_THD;
  272. }
  273. static int get_proximity_threshold(struct cm36686_data *data)
  274. {
  275. if (ps_reg_init_setting[PS_CANCEL][CMD] <= CAL_SKIP_ADC) { /* SKIP */
  276. ps_reg_init_setting[PS_CANCEL][CMD] = 0;
  277. data->uProxCalResult = 2;
  278. pr_info("%s: crosstalk <= %d, skip calibration\n", __func__, CAL_SKIP_ADC);
  279. return ERROR;
  280. } else if (ps_reg_init_setting[PS_CANCEL][CMD] <= CAL_FAIL_ADC) { /* CANCELATION */
  281. ps_reg_init_setting[PS_CANCEL][CMD] = ps_reg_init_setting[PS_CANCEL][CMD];
  282. data->uProxCalResult = 1;
  283. } else { /* FAIL */
  284. ps_reg_init_setting[PS_CANCEL][CMD] = 0;
  285. data->uProxCalResult = 0;
  286. pr_info("%s: crosstalk > %d, calibration failed\n", __func__, CAL_FAIL_ADC);
  287. return ERROR;
  288. }
  289. pr_info("%s: crosstalk_offset = %u",
  290. __func__, ps_reg_init_setting[PS_CANCEL][CMD]);
  291. return SUCCESS;
  292. }
  293. static int proximity_open_cancelation(struct cm36686_data *data)
  294. {
  295. struct file *cancel_filp = NULL;
  296. int err = 0;
  297. mm_segment_t old_fs;
  298. old_fs = get_fs();
  299. set_fs(KERNEL_DS);
  300. cancel_filp = filp_open(CANCELATION_FILE_PATH, O_RDONLY, 0666);
  301. if (IS_ERR(cancel_filp)) {
  302. err = PTR_ERR(cancel_filp);
  303. if (err != -ENOENT)
  304. pr_err("[SENSOR] %s: Can't open cancelation file\n",
  305. __func__);
  306. set_fs(old_fs);
  307. return err;
  308. }
  309. err = cancel_filp->f_op->read(cancel_filp,
  310. (char *)&ps_reg_init_setting[PS_CANCEL][CMD],
  311. sizeof(u16), &cancel_filp->f_pos);
  312. if (err != sizeof(u16)) {
  313. pr_err("%s: Can't read the cancel data from file\n", __func__);
  314. err = -EIO;
  315. }
  316. /*If there is an offset cal data. */
  317. if (ps_reg_init_setting[PS_CANCEL][CMD] != 0) {
  318. if (data->pdata->cancel_hi_thd) {
  319. ps_reg_init_setting[PS_THD_HIGH][CMD] =
  320. data->pdata->cancel_hi_thd;
  321. } else
  322. ps_reg_init_setting[PS_THD_HIGH][CMD] = CANCEL_HI_THD;
  323. if (data->pdata->cancel_low_thd) {
  324. ps_reg_init_setting[PS_THD_LOW][CMD] =
  325. data->pdata->cancel_low_thd;
  326. } else
  327. ps_reg_init_setting[PS_THD_LOW][CMD] = CANCEL_LOW_THD;
  328. }
  329. pr_err("[SENSOR] %s: prox_cal = 0x%x, ps_high_thresh = 0x%x, ps_low_thresh = 0x%x\n",
  330. __func__,
  331. ps_reg_init_setting[PS_CANCEL][CMD],
  332. ps_reg_init_setting[PS_THD_HIGH][CMD],
  333. ps_reg_init_setting[PS_THD_LOW][CMD]);
  334. filp_close(cancel_filp, current->files);
  335. set_fs(old_fs);
  336. return err;
  337. }
  338. static int proximity_store_cancelation(struct device *dev, bool do_calib)
  339. {
  340. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  341. struct file *cancel_filp = NULL;
  342. mm_segment_t old_fs;
  343. int err = 0;
  344. u16 ps_data = 0;
  345. if (do_calib) {
  346. mutex_lock(&cm36686->read_lock);
  347. cm36686_i2c_read_word(cm36686,
  348. REG_PS_DATA, &ps_data);
  349. ps_reg_init_setting[PS_CANCEL][CMD] = ps_data;
  350. mutex_unlock(&cm36686->read_lock);
  351. err = get_proximity_threshold(cm36686);
  352. if(err != ERROR) {
  353. if (cm36686->pdata->cancel_hi_thd) {
  354. ps_reg_init_setting[PS_THD_HIGH][CMD] =
  355. cm36686->pdata->cancel_hi_thd;
  356. } else
  357. ps_reg_init_setting[PS_THD_HIGH][CMD] = CANCEL_HI_THD;
  358. if (cm36686->pdata->cancel_low_thd) {
  359. ps_reg_init_setting[PS_THD_LOW][CMD] =
  360. cm36686->pdata->cancel_low_thd;
  361. } else
  362. ps_reg_init_setting[PS_THD_LOW][CMD] = DEFUALT_LOW_THD;
  363. } else {
  364. set_default_proximity_threshold(cm36686);
  365. }
  366. } else { /* reset */
  367. ps_reg_init_setting[PS_CANCEL][CMD] = 0;
  368. set_default_proximity_threshold(cm36686);
  369. }
  370. err = cm36686_i2c_write_word(cm36686, REG_PS_CANC,
  371. ps_reg_init_setting[PS_CANCEL][CMD]);
  372. if (err < 0)
  373. pr_err("%s: cm36686_ps_canc_reg is failed. %d\n", __func__,
  374. err);
  375. err = cm36686_i2c_write_word(cm36686, REG_PS_THD_HIGH,
  376. ps_reg_init_setting[PS_THD_HIGH][CMD]);
  377. if (err < 0)
  378. pr_err("%s: cm36686_ps_high_reg is failed. %d\n", __func__,
  379. err);
  380. err = cm36686_i2c_write_word(cm36686, REG_PS_THD_LOW,
  381. ps_reg_init_setting[PS_THD_LOW][CMD]);
  382. if (err < 0)
  383. pr_err("%s: cm36686_ps_low_reg is failed. %d\n", __func__,
  384. err);
  385. pr_info("%s: prox_cal = 0x%x, ps_high_thresh = 0x%x, ps_low_thresh = 0x%x\n",
  386. __func__,
  387. ps_reg_init_setting[PS_CANCEL][CMD],
  388. ps_reg_init_setting[PS_THD_HIGH][CMD],
  389. ps_reg_init_setting[PS_THD_LOW][CMD]);
  390. old_fs = get_fs();
  391. set_fs(KERNEL_DS);
  392. cancel_filp = filp_open(CANCELATION_FILE_PATH,
  393. O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, 0666);
  394. if (IS_ERR(cancel_filp)) {
  395. pr_err("[SENSOR] %s: Can't open cancelation file\n", __func__);
  396. set_fs(old_fs);
  397. err = PTR_ERR(cancel_filp);
  398. return err;
  399. }
  400. err = cancel_filp->f_op->write(cancel_filp,
  401. (char *)&ps_reg_init_setting[PS_CANCEL][CMD],
  402. sizeof(u16), &cancel_filp->f_pos);
  403. if (err != sizeof(u16)) {
  404. pr_err("%s: Can't write the cancel data to file\n", __func__);
  405. err = -EIO;
  406. }
  407. filp_close(cancel_filp, current->files);
  408. set_fs(old_fs);
  409. if (!do_calib) /* delay for clearing */
  410. msleep(150);
  411. return err;
  412. }
  413. static ssize_t proximity_cancel_store(struct device *dev,
  414. struct device_attribute *attr, const char *buf, size_t size)
  415. {
  416. bool do_calib;
  417. int err;
  418. if (sysfs_streq(buf, "1")) /* calibrate cancelation value */
  419. do_calib = true;
  420. else if (sysfs_streq(buf, "0")) /* reset cancelation value */
  421. do_calib = false;
  422. else {
  423. pr_debug("[SENSOR] %s: invalid value %d\n", __func__, *buf);
  424. return -EINVAL;
  425. }
  426. err = proximity_store_cancelation(dev, do_calib);
  427. if (err < 0) {
  428. pr_err("[SENSOR] %s: proximity_store_cancelation() failed\n",
  429. __func__);
  430. return err;
  431. }
  432. return size;
  433. }
  434. static ssize_t proximity_cancel_show(struct device *dev,
  435. struct device_attribute *attr, char *buf)
  436. {
  437. return snprintf(buf, PAGE_SIZE, "%u,%u,%u\n",
  438. ps_reg_init_setting[PS_CANCEL][CMD],
  439. ps_reg_init_setting[PS_THD_HIGH][CMD],
  440. ps_reg_init_setting[PS_THD_LOW][CMD]);
  441. }
  442. static ssize_t proximity_cancel_pass_show(struct device *dev,
  443. struct device_attribute *attr, char *buf)
  444. {
  445. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  446. pr_info("%s, %u\n", __func__, cm36686->uProxCalResult);
  447. return snprintf(buf, PAGE_SIZE, "%u\n", cm36686->uProxCalResult);
  448. }
  449. #endif
  450. #if defined(CONFIG_SENSORS_CM36686_LEDA_EN_GPIO)
  451. static int cm36686_setup_leden_gpio(struct cm36686_data *cm36686)
  452. {
  453. int rc;
  454. struct cm36686_platform_data *pdata = cm36686->pdata;
  455. rc = gpio_request(pdata->leden_gpio, "prox_en");
  456. if (rc < 0) {
  457. pr_err("%s: gpio %d request failed (%d)\n",
  458. __func__, pdata->leden_gpio, rc);
  459. }
  460. rc = gpio_direction_output(pdata->leden_gpio, 1);
  461. if (rc)
  462. pr_err("%s: unable to set_direction for prox_en [%d]\n",
  463. __func__, pdata->leden_gpio);
  464. return rc;
  465. }
  466. static int cm36686_leden_gpio_onoff(struct cm36686_data *cm36686, bool onoff)
  467. {
  468. struct cm36686_platform_data *pdata = cm36686->pdata;
  469. gpio_set_value(pdata->leden_gpio, onoff);
  470. if (onoff)
  471. msleep(20);
  472. return 0;
  473. }
  474. #else
  475. static int prox_led_onoff(struct cm36686_data *cm36686, bool onoff)
  476. {
  477. #if !defined(CONFIG_SEC_ATLANTIC3G_COMMON)
  478. struct regulator *leda;
  479. leda = devm_regulator_get(&cm36686->i2c_client->dev, "reg_leda");
  480. if (IS_ERR(leda)) {
  481. pr_err("%s: regulator pointer null leda fail\n", __func__);
  482. return -ENOMEM;
  483. }
  484. if (onoff)
  485. regulator_enable(leda);
  486. else
  487. regulator_disable(leda);
  488. devm_regulator_put(leda);
  489. msleep(20);
  490. return 0;
  491. #else
  492. return 0;
  493. #endif
  494. }
  495. #endif
  496. static ssize_t proximity_enable_store(struct device *dev,
  497. struct device_attribute *attr, const char *buf, size_t size)
  498. {
  499. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  500. bool new_value;
  501. if (sysfs_streq(buf, "1"))
  502. new_value = true;
  503. else if (sysfs_streq(buf, "0"))
  504. new_value = false;
  505. else {
  506. pr_err("[SENSOR] %s: invalid value %d\n", __func__, *buf);
  507. return -EINVAL;
  508. }
  509. mutex_lock(&cm36686->power_lock);
  510. pr_info("[SENSOR] %s, new_value = %d\n", __func__, new_value);
  511. if (new_value && !(cm36686->power_state & PROXIMITY_ENABLED)) {
  512. u8 val = 1;
  513. int i;
  514. #ifdef CM36686_CANCELATION
  515. int err = 0;
  516. #endif
  517. cm36686->power_state |= PROXIMITY_ENABLED;
  518. #if defined(CONFIG_SENSORS_CM36686_LEDA_EN_GPIO)
  519. cm36686_leden_gpio_onoff(cm36686, 1);
  520. #else
  521. prox_led_onoff(cm36686, 1);
  522. #endif
  523. #ifdef CM36686_CANCELATION
  524. /* open cancelation data */
  525. err = proximity_open_cancelation(cm36686);
  526. if (err < 0 && err != -ENOENT)
  527. pr_err("[SENSOR] %s: proximity_open_cancelation() failed\n",
  528. __func__);
  529. #endif
  530. /* enable settings */
  531. for (i = 0; i < PS_REG_NUM; i++) {
  532. cm36686_i2c_write_word(cm36686,
  533. ps_reg_init_setting[i][REG_ADDR],
  534. ps_reg_init_setting[i][CMD]);
  535. }
  536. val = gpio_get_value(cm36686->pdata->irq);
  537. /* 0 is close, 1 is far */
  538. input_report_abs(cm36686->proximity_input_dev,
  539. ABS_DISTANCE, val);
  540. input_sync(cm36686->proximity_input_dev);
  541. enable_irq(cm36686->irq);
  542. enable_irq_wake(cm36686->irq);
  543. } else if (!new_value && (cm36686->power_state & PROXIMITY_ENABLED)) {
  544. cm36686->power_state &= ~PROXIMITY_ENABLED;
  545. disable_irq_wake(cm36686->irq);
  546. disable_irq(cm36686->irq);
  547. /* disable settings */
  548. cm36686_i2c_write_word(cm36686, REG_PS_CONF1, 0x0001);
  549. #if defined(CONFIG_SENSORS_CM36686_LEDA_EN_GPIO)
  550. cm36686_leden_gpio_onoff(cm36686, 0);
  551. #else
  552. prox_led_onoff(cm36686, 0);
  553. #endif
  554. }
  555. mutex_unlock(&cm36686->power_lock);
  556. return size;
  557. }
  558. static ssize_t proximity_enable_show(struct device *dev,
  559. struct device_attribute *attr, char *buf)
  560. {
  561. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  562. return snprintf(buf, PAGE_SIZE, "%d\n",
  563. (cm36686->power_state & PROXIMITY_ENABLED) ? 1 : 0);
  564. }
  565. static DEVICE_ATTR(poll_delay, S_IRUGO | S_IWUSR | S_IWGRP,
  566. cm36686_poll_delay_show, cm36686_poll_delay_store);
  567. static struct device_attribute dev_attr_light_enable =
  568. __ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  569. light_enable_show, light_enable_store);
  570. static struct device_attribute dev_attr_proximity_enable =
  571. __ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  572. proximity_enable_show, proximity_enable_store);
  573. static struct attribute *light_sysfs_attrs[] = {
  574. &dev_attr_light_enable.attr,
  575. &dev_attr_poll_delay.attr,
  576. NULL
  577. };
  578. static struct attribute_group light_attribute_group = {
  579. .attrs = light_sysfs_attrs,
  580. };
  581. static struct attribute *proximity_sysfs_attrs[] = {
  582. &dev_attr_proximity_enable.attr,
  583. NULL
  584. };
  585. static struct attribute_group proximity_attribute_group = {
  586. .attrs = proximity_sysfs_attrs,
  587. };
  588. /* sysfs for vendor & name */
  589. static ssize_t cm36686_vendor_show(struct device *dev,
  590. struct device_attribute *attr, char *buf)
  591. {
  592. return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR);
  593. }
  594. static ssize_t cm36686_name_show(struct device *dev,
  595. struct device_attribute *attr, char *buf)
  596. {
  597. return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_ID);
  598. }
  599. static struct device_attribute dev_attr_prox_sensor_vendor =
  600. __ATTR(vendor, S_IRUSR | S_IRGRP, cm36686_vendor_show, NULL);
  601. static struct device_attribute dev_attr_light_sensor_vendor =
  602. __ATTR(vendor, S_IRUSR | S_IRGRP, cm36686_vendor_show, NULL);
  603. static struct device_attribute dev_attr_prox_sensor_name =
  604. __ATTR(name, S_IRUSR | S_IRGRP, cm36686_name_show, NULL);
  605. static struct device_attribute dev_attr_light_sensor_name =
  606. __ATTR(name, S_IRUSR | S_IRGRP, cm36686_name_show, NULL);
  607. /* proximity sysfs */
  608. static ssize_t proximity_avg_show(struct device *dev,
  609. struct device_attribute *attr, char *buf)
  610. {
  611. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  612. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", cm36686->avg[0],
  613. cm36686->avg[1], cm36686->avg[2]);
  614. }
  615. static ssize_t proximity_avg_store(struct device *dev,
  616. struct device_attribute *attr, const char *buf, size_t size)
  617. {
  618. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  619. bool new_value = false;
  620. if (sysfs_streq(buf, "1"))
  621. new_value = true;
  622. else if (sysfs_streq(buf, "0"))
  623. new_value = false;
  624. else {
  625. pr_err("[SENSOR] %s, invalid value %d\n", __func__, *buf);
  626. return -EINVAL;
  627. }
  628. pr_info("[SENSOR] %s, average enable = %d\n", __func__, new_value);
  629. mutex_lock(&cm36686->power_lock);
  630. if (new_value) {
  631. if (!(cm36686->power_state & PROXIMITY_ENABLED)) {
  632. #if defined(CONFIG_SENSORS_CM36686_LEDA_EN_GPIO)
  633. cm36686_leden_gpio_onoff(cm36686, 1);
  634. #else
  635. prox_led_onoff(cm36686, 1);
  636. #endif
  637. cm36686_i2c_write_word(cm36686, REG_PS_CONF1,
  638. ps_reg_init_setting[PS_CONF1][CMD]);
  639. }
  640. hrtimer_start(&cm36686->prox_timer, cm36686->prox_poll_delay,
  641. HRTIMER_MODE_REL);
  642. } else if (!new_value) {
  643. hrtimer_cancel(&cm36686->prox_timer);
  644. cancel_work_sync(&cm36686->work_prox);
  645. if (!(cm36686->power_state & PROXIMITY_ENABLED)) {
  646. cm36686_i2c_write_word(cm36686, REG_PS_CONF1,
  647. 0x0001);
  648. #if defined(CONFIG_SENSORS_CM36686_LEDA_EN_GPIO)
  649. cm36686_leden_gpio_onoff(cm36686, 0);
  650. #else
  651. prox_led_onoff(cm36686, 0);
  652. #endif
  653. }
  654. }
  655. mutex_unlock(&cm36686->power_lock);
  656. return size;
  657. }
  658. static ssize_t proximity_state_show(struct device *dev,
  659. struct device_attribute *attr, char *buf)
  660. {
  661. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  662. u16 ps_data;
  663. mutex_lock(&cm36686->power_lock);
  664. if (!(cm36686->power_state & PROXIMITY_ENABLED)) {
  665. #if defined(CONFIG_SENSORS_CM36686_LEDA_EN_GPIO)
  666. cm36686_leden_gpio_onoff(cm36686, 1);
  667. #else
  668. prox_led_onoff(cm36686, 1);
  669. #endif
  670. cm36686_i2c_write_word(cm36686, REG_PS_CONF1,
  671. ps_reg_init_setting[PS_CONF1][CMD]);
  672. }
  673. mutex_lock(&cm36686->read_lock);
  674. cm36686_i2c_read_word(cm36686, REG_PS_DATA, &ps_data);
  675. mutex_unlock(&cm36686->read_lock);
  676. if (!(cm36686->power_state & PROXIMITY_ENABLED)) {
  677. cm36686_i2c_write_word(cm36686, REG_PS_CONF1, 0x0001);
  678. #if defined(CONFIG_SENSORS_CM36686_LEDA_EN_GPIO)
  679. cm36686_leden_gpio_onoff(cm36686, 0);
  680. #else
  681. prox_led_onoff(cm36686, 0);
  682. #endif
  683. }
  684. mutex_unlock(&cm36686->power_lock);
  685. return snprintf(buf, PAGE_SIZE, "%u\n", ps_data);
  686. }
  687. static ssize_t proximity_thresh_high_show(struct device *dev,
  688. struct device_attribute *attr, char *buf)
  689. {
  690. pr_info("%s = %u,%u\n", __func__,
  691. ps_reg_init_setting[PS_THD_HIGH][CMD],
  692. ps_reg_init_setting[PS_THD_LOW][CMD]);
  693. return snprintf(buf, PAGE_SIZE, "%u,%u\n",
  694. ps_reg_init_setting[PS_THD_HIGH][CMD],
  695. ps_reg_init_setting[PS_THD_LOW][CMD]);
  696. }
  697. static ssize_t proximity_thresh_high_store(struct device *dev,
  698. struct device_attribute *attr, const char *buf, size_t size)
  699. {
  700. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  701. u16 thresh_value = ps_reg_init_setting[PS_THD_HIGH][CMD];
  702. int err;
  703. err = kstrtou16(buf, 10, &thresh_value);
  704. if (err < 0)
  705. pr_err("%s, kstrtoint failed.", __func__);
  706. if (thresh_value > 2) {
  707. ps_reg_init_setting[PS_THD_HIGH][CMD] = thresh_value;
  708. err = cm36686_i2c_write_word(cm36686, REG_PS_THD_HIGH,
  709. ps_reg_init_setting[PS_THD_HIGH][CMD]);
  710. if (err < 0)
  711. pr_err("%s: cm36686_ps_high_reg is failed. %d\n",
  712. __func__, err);
  713. pr_info("%s, new high threshold = 0x%x\n",
  714. __func__, thresh_value);
  715. msleep(150);
  716. } else
  717. pr_err("%s, wrong high threshold value(0x%x)!!\n",
  718. __func__, thresh_value);
  719. return size;
  720. }
  721. static ssize_t proximity_thresh_low_show(struct device *dev,
  722. struct device_attribute *attr, char *buf)
  723. {
  724. pr_info("%s = %u,%u\n", __func__,
  725. ps_reg_init_setting[PS_THD_HIGH][CMD],
  726. ps_reg_init_setting[PS_THD_LOW][CMD]);
  727. return snprintf(buf, PAGE_SIZE, "%u,%u\n",
  728. ps_reg_init_setting[PS_THD_HIGH][CMD],
  729. ps_reg_init_setting[PS_THD_LOW][CMD]);
  730. }
  731. static ssize_t proximity_thresh_low_store(struct device *dev,
  732. struct device_attribute *attr, const char *buf, size_t size)
  733. {
  734. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  735. u16 thresh_value = ps_reg_init_setting[PS_THD_LOW][CMD];
  736. int err;
  737. err = kstrtou16(buf, 10, &thresh_value);
  738. if (err < 0)
  739. pr_err("%s, kstrtoint failed.", __func__);
  740. if (thresh_value > 2) {
  741. ps_reg_init_setting[PS_THD_LOW][CMD] = thresh_value;
  742. err = cm36686_i2c_write_word(cm36686, REG_PS_THD_LOW,
  743. ps_reg_init_setting[PS_THD_LOW][CMD]);
  744. if (err < 0)
  745. pr_err("%s: cm36686_ps_low_reg is failed. %d\n",
  746. __func__, err);
  747. pr_info("%s, new low threshold = 0x%x\n",
  748. __func__, thresh_value);
  749. msleep(150);
  750. } else
  751. pr_err("%s, wrong low threshold value(0x%x)!!\n",
  752. __func__, thresh_value);
  753. return size;
  754. }
  755. #ifdef CM36686_CANCELATION
  756. static DEVICE_ATTR(prox_cal, S_IRUGO | S_IWUSR | S_IWGRP,
  757. proximity_cancel_show, proximity_cancel_store);
  758. static DEVICE_ATTR(prox_offset_pass, S_IRUGO, proximity_cancel_pass_show, NULL);
  759. #endif
  760. static DEVICE_ATTR(prox_avg, S_IRUGO | S_IWUSR | S_IWGRP,
  761. proximity_avg_show, proximity_avg_store);
  762. static DEVICE_ATTR(state, S_IRUGO, proximity_state_show, NULL);
  763. static struct device_attribute dev_attr_prox_raw = __ATTR(raw_data,
  764. S_IRUGO, proximity_state_show, NULL);
  765. static DEVICE_ATTR(thresh_high, S_IRUGO | S_IWUSR | S_IWGRP,
  766. proximity_thresh_high_show, proximity_thresh_high_store);
  767. static DEVICE_ATTR(thresh_low, S_IRUGO | S_IWUSR | S_IWGRP,
  768. proximity_thresh_low_show, proximity_thresh_low_store);
  769. static struct device_attribute *prox_sensor_attrs[] = {
  770. &dev_attr_prox_sensor_vendor,
  771. &dev_attr_prox_sensor_name,
  772. #ifdef CM36686_CANCELATION
  773. &dev_attr_prox_cal,
  774. &dev_attr_prox_offset_pass,
  775. #endif
  776. &dev_attr_prox_avg,
  777. &dev_attr_state,
  778. &dev_attr_thresh_high,
  779. &dev_attr_thresh_low,
  780. &dev_attr_prox_raw,
  781. NULL,
  782. };
  783. /* light sysfs */
  784. static ssize_t light_lux_show(struct device *dev,
  785. struct device_attribute *attr, char *buf)
  786. {
  787. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  788. return snprintf(buf, PAGE_SIZE, "%u,%u\n", cm36686->als_data,
  789. cm36686->white_data);
  790. }
  791. static ssize_t light_data_show(struct device *dev,
  792. struct device_attribute *attr, char *buf)
  793. {
  794. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  795. #ifdef cm36686_DEBUG
  796. pr_info("%s = %u,%u\n", __func__, cm36686->als_data,
  797. cm36686->white_data);
  798. #endif
  799. return snprintf(buf, PAGE_SIZE, "%u,%u\n", cm36686->als_data,
  800. cm36686->white_data);
  801. }
  802. static DEVICE_ATTR(lux, S_IRUGO, light_lux_show, NULL);
  803. static DEVICE_ATTR(raw_data, S_IRUGO, light_data_show, NULL);
  804. static struct device_attribute *light_sensor_attrs[] = {
  805. &dev_attr_light_sensor_vendor,
  806. &dev_attr_light_sensor_name,
  807. &dev_attr_lux,
  808. &dev_attr_raw_data,
  809. NULL,
  810. };
  811. /* interrupt happened due to transition/change of near/far proximity state */
  812. irqreturn_t cm36686_irq_thread_fn(int irq, void *data)
  813. {
  814. struct cm36686_data *cm36686 = data;
  815. u8 val = 1;
  816. u16 ps_data = 0;
  817. #ifdef cm36686_DEBUG
  818. static int count;
  819. pr_info("%s\n", __func__);
  820. #endif
  821. val = gpio_get_value(cm36686->pdata->irq);
  822. cm36686_i2c_read_word(cm36686, REG_PS_DATA, &ps_data);
  823. #ifdef cm36686_DEBUG
  824. pr_info("[SENSOR] %s: count = %d\n", __func__, count++);
  825. #endif
  826. if (cm36686->power_state & PROXIMITY_ENABLED) {
  827. /* 0 is close, 1 is far */
  828. input_report_abs(cm36686->proximity_input_dev, ABS_DISTANCE,
  829. val);
  830. input_sync(cm36686->proximity_input_dev);
  831. }
  832. wake_lock_timeout(&cm36686->prx_wake_lock, 3 * HZ);
  833. pr_info("%s: val = %u, ps_data = %u (close:0, far:1)\n",
  834. __func__, val, ps_data);
  835. return IRQ_HANDLED;
  836. }
  837. static int cm36686_setup_reg(struct cm36686_data *cm36686)
  838. {
  839. int err = 0, i = 0;
  840. u16 tmp = 0;
  841. /* ALS initialization */
  842. err = cm36686_i2c_write_word(cm36686,
  843. als_reg_setting[0][0],
  844. als_reg_setting[0][1]);
  845. if (err < 0) {
  846. pr_err("[SENSOR] %s: cm36686_als_reg is failed. %d\n", __func__,
  847. err);
  848. return err;
  849. }
  850. /* PS initialization */
  851. for (i = 0; i < PS_REG_NUM; i++) {
  852. err = cm36686_i2c_write_word(cm36686,
  853. ps_reg_init_setting[i][REG_ADDR],
  854. ps_reg_init_setting[i][CMD]);
  855. if (err < 0) {
  856. pr_err("[SENSOR] %s: cm36686_ps_reg is failed. %d\n",
  857. __func__, err);
  858. return err;
  859. }
  860. }
  861. /* printing the inital proximity value with no contact */
  862. msleep(50);
  863. mutex_lock(&cm36686->read_lock);
  864. err = cm36686_i2c_read_word(cm36686, REG_PS_DATA, &tmp);
  865. mutex_unlock(&cm36686->read_lock);
  866. if (err < 0) {
  867. pr_err("[SENSOR] %s: read ps_data failed\n", __func__);
  868. err = -EIO;
  869. }
  870. /* turn off */
  871. cm36686_i2c_write_word(cm36686, REG_CS_CONF1, 0x0001);
  872. cm36686_i2c_write_word(cm36686, REG_PS_CONF1, 0x0001);
  873. cm36686_i2c_write_word(cm36686, REG_PS_CONF3, 0x0000);
  874. return err;
  875. }
  876. static int cm36686_setup_irq(struct cm36686_data *cm36686)
  877. {
  878. int rc;
  879. struct cm36686_platform_data *pdata = cm36686->pdata;
  880. rc = gpio_request(pdata->irq, "gpio_proximity_out");
  881. if (rc < 0) {
  882. pr_err("%s: gpio %d request failed (%d)\n",
  883. __func__, pdata->irq, rc);
  884. return rc;
  885. }
  886. rc = gpio_direction_input(pdata->irq);
  887. if (rc < 0) {
  888. pr_err("%s: failed to set gpio %d as input (%d)\n",
  889. __func__, pdata->irq, rc);
  890. goto err_gpio_direction_input;
  891. }
  892. cm36686->irq = gpio_to_irq(pdata->irq);
  893. rc = request_threaded_irq(cm36686->irq, NULL,
  894. cm36686_irq_thread_fn,
  895. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  896. "proximity_int", cm36686);
  897. if (rc < 0) {
  898. pr_err("%s: request_irq(%d) failed for gpio %d (%d)\n",
  899. __func__, cm36686->irq, pdata->irq, rc);
  900. goto err_request_irq;
  901. }
  902. /* start with interrupts disabled */
  903. disable_irq(cm36686->irq);
  904. goto done;
  905. err_request_irq:
  906. err_gpio_direction_input:
  907. gpio_free(pdata->irq);
  908. done:
  909. return rc;
  910. }
  911. /* This function is for light sensor. It operates every a few seconds.
  912. * It asks for work to be done on a thread because i2c needs a thread
  913. * context (slow and blocking) and then reschedules the timer to run again.
  914. */
  915. static enum hrtimer_restart cm36686_light_timer_func(struct hrtimer *timer)
  916. {
  917. struct cm36686_data *cm36686
  918. = container_of(timer, struct cm36686_data, light_timer);
  919. queue_work(cm36686->light_wq, &cm36686->work_light);
  920. hrtimer_forward_now(&cm36686->light_timer, cm36686->light_poll_delay);
  921. return HRTIMER_RESTART;
  922. }
  923. static void cm36686_work_func_light(struct work_struct *work)
  924. {
  925. struct cm36686_data *cm36686 = container_of(work, struct cm36686_data,
  926. work_light);
  927. mutex_lock(&cm36686->read_lock);
  928. cm36686_i2c_read_word(cm36686, REG_ALS_DATA, &cm36686->als_data);
  929. cm36686_i2c_read_word(cm36686, REG_WHITE_DATA, &cm36686->white_data);
  930. mutex_unlock(&cm36686->read_lock);
  931. input_report_rel(cm36686->light_input_dev, REL_DIAL,
  932. cm36686->als_data + 1);
  933. input_report_rel(cm36686->light_input_dev, REL_WHEEL,
  934. cm36686->white_data + 1);
  935. input_sync(cm36686->light_input_dev);
  936. if (cm36686->count_log_time >= LIGHT_LOG_TIME) {
  937. pr_info("%s, %u,%u\n", __func__,
  938. cm36686->als_data, cm36686->white_data);
  939. cm36686->count_log_time = 0;
  940. } else
  941. cm36686->count_log_time++;
  942. #ifdef cm36686_DEBUG
  943. pr_info("%s, %u,%u\n", __func__,
  944. cm36686->als_data, cm36686->white_data);
  945. #endif
  946. }
  947. static void proxsensor_get_avg_val(struct cm36686_data *cm36686)
  948. {
  949. int min = 0, max = 0, avg = 0;
  950. int i;
  951. u16 ps_data = 0;
  952. for (i = 0; i < PROX_READ_NUM; i++) {
  953. msleep(40);
  954. cm36686_i2c_read_word(cm36686, REG_PS_DATA,
  955. &ps_data);
  956. avg += ps_data;
  957. if (!i)
  958. min = ps_data;
  959. else if (ps_data < min)
  960. min = ps_data;
  961. if (ps_data > max)
  962. max = ps_data;
  963. }
  964. avg /= PROX_READ_NUM;
  965. cm36686->avg[0] = min;
  966. cm36686->avg[1] = avg;
  967. cm36686->avg[2] = max;
  968. }
  969. static void cm36686_work_func_prox(struct work_struct *work)
  970. {
  971. struct cm36686_data *cm36686 = container_of(work, struct cm36686_data,
  972. work_prox);
  973. proxsensor_get_avg_val(cm36686);
  974. }
  975. static enum hrtimer_restart cm36686_prox_timer_func(struct hrtimer *timer)
  976. {
  977. struct cm36686_data *cm36686
  978. = container_of(timer, struct cm36686_data, prox_timer);
  979. queue_work(cm36686->prox_wq, &cm36686->work_prox);
  980. hrtimer_forward_now(&cm36686->prox_timer, cm36686->prox_poll_delay);
  981. return HRTIMER_RESTART;
  982. }
  983. #ifdef CONFIG_OF
  984. /* device tree parsing function */
  985. static int cm36686_parse_dt(struct device *dev,
  986. struct cm36686_platform_data *pdata)
  987. {
  988. struct device_node *np = dev->of_node;
  989. enum of_gpio_flags flags;
  990. int ret;
  991. pdata->irq = of_get_named_gpio_flags(np, "cm36686,irq_gpio", 0, &flags);
  992. if (pdata->irq < 0) {
  993. pr_err("[SENSOR]: %s - get prox_int error\n", __func__);
  994. return -ENODEV;
  995. }
  996. #if defined(CONFIG_SENSORS_CM36686_LEDA_EN_GPIO)
  997. pdata->leden_gpio = of_get_named_gpio_flags(np, "cm36686,leden_gpio", 0,
  998. &flags);
  999. if (pdata->leden_gpio < 0) {
  1000. pr_err("[SENSOR]: %s - get prox_leden_gpio error\n", __func__);
  1001. return -ENODEV;
  1002. }
  1003. #endif
  1004. ret = of_property_read_u32(np, "cm36686,default_hi_thd",
  1005. &pdata->default_hi_thd);
  1006. if (ret < 0) {
  1007. pr_err("[SENSOR]: %s - Cannot set default_hi_thd through DTSI\n",
  1008. __func__);
  1009. pdata->default_hi_thd = DEFUALT_HI_THD;
  1010. }
  1011. ret = of_property_read_u32(np, "cm36686,default_low_thd",
  1012. &pdata->default_low_thd);
  1013. if (ret < 0) {
  1014. pr_err("[SENSOR]: %s - Cannot set default_low_thd through DTSI\n",
  1015. __func__);
  1016. pdata->default_low_thd = DEFUALT_LOW_THD;
  1017. }
  1018. ret = of_property_read_u32(np, "cm36686,cancel_hi_thd",
  1019. &pdata->cancel_hi_thd);
  1020. if (ret < 0) {
  1021. pr_err("[SENSOR]: %s - Cannot set cancel_hi_thd through DTSI\n",
  1022. __func__);
  1023. pdata->cancel_hi_thd = CANCEL_HI_THD;
  1024. }
  1025. ret = of_property_read_u32(np, "cm36686,cancel_low_thd",
  1026. &pdata->cancel_low_thd);
  1027. if (ret < 0) {
  1028. pr_err("[SENSOR]: %s - Cannot set cancel_low_thd through DTSI\n",
  1029. __func__);
  1030. pdata->cancel_low_thd = CANCEL_LOW_THD;
  1031. }
  1032. ps_reg_init_setting[2][CMD] = pdata->default_low_thd;
  1033. ps_reg_init_setting[3][CMD] = pdata->default_hi_thd;
  1034. return 0;
  1035. }
  1036. #else
  1037. static int cm36686_parse_dt(struct device *dev, struct cm36686_platform_data)
  1038. {
  1039. return -ENODEV;
  1040. }
  1041. #endif
  1042. static int prox_regulator_onoff(struct device *dev, bool onoff)
  1043. {
  1044. struct regulator *vdd;
  1045. struct regulator *vio;
  1046. pr_err("%s %s\n", __func__, (onoff) ? "on" : "off");
  1047. vdd = devm_regulator_get(dev, "reg_vdd");
  1048. if (IS_ERR(vdd)) {
  1049. pr_err("%s: cannot get vdd\n", __func__);
  1050. return -ENOMEM;
  1051. }
  1052. vio = devm_regulator_get(dev, "reg_vio");
  1053. if (IS_ERR(vio)) {
  1054. pr_err("%s: cannot get vio\n", __func__);
  1055. return -ENOMEM;
  1056. }
  1057. if (onoff) {
  1058. regulator_enable(vdd);
  1059. msleep(20);
  1060. regulator_enable(vio);
  1061. msleep(20);
  1062. } else {
  1063. regulator_disable(vdd);
  1064. msleep(20);
  1065. regulator_disable(vio);
  1066. msleep(20);
  1067. }
  1068. devm_regulator_put(vdd);
  1069. devm_regulator_put(vio);
  1070. msleep(20);
  1071. return 0;
  1072. }
  1073. static int cm36686_i2c_probe(struct i2c_client *client,
  1074. const struct i2c_device_id *id)
  1075. {
  1076. int ret = -ENODEV;
  1077. int err;
  1078. struct cm36686_data *cm36686 = NULL;
  1079. struct cm36686_platform_data *pdata = NULL;
  1080. pr_info("[SENSOR] %s: Probe Start!\n", __func__);
  1081. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1082. pr_err("[SENSOR] %s: i2c functionality check failed!\n",
  1083. __func__);
  1084. return ret;
  1085. }
  1086. cm36686 = kzalloc(sizeof(struct cm36686_data), GFP_KERNEL);
  1087. if (!cm36686) {
  1088. pr_err("[SENSOR] %s: failed to alloc memory for RGB sensor module data\n",
  1089. __func__);
  1090. return -ENOMEM;
  1091. }
  1092. if (client->dev.of_node) {
  1093. pdata = devm_kzalloc(&client->dev,
  1094. sizeof(struct cm36686_platform_data), GFP_KERNEL);
  1095. if (!pdata) {
  1096. dev_err(&client->dev, "Failed to allocate memory\n");
  1097. if (cm36686)
  1098. kfree(cm36686);
  1099. return -ENOMEM;
  1100. }
  1101. err = cm36686_parse_dt(&client->dev, pdata);
  1102. if (err)
  1103. goto err_devicetree;
  1104. } else
  1105. pdata = client->dev.platform_data;
  1106. if (!pdata) {
  1107. pr_err("%s: missing pdata!\n", __func__);
  1108. if (cm36686)
  1109. kfree(cm36686);
  1110. return ret;
  1111. }
  1112. prox_regulator_onoff(&client->dev, 1);
  1113. cm36686->pdata = pdata;
  1114. cm36686->i2c_client = client;
  1115. i2c_set_clientdata(client, cm36686);
  1116. mutex_init(&cm36686->power_lock);
  1117. mutex_init(&cm36686->read_lock);
  1118. /* wake lock init for proximity sensor */
  1119. wake_lock_init(&cm36686->prx_wake_lock, WAKE_LOCK_SUSPEND,
  1120. "prx_wake_lock");
  1121. #if defined(CONFIG_SENSORS_CM36686_LEDA_EN_GPIO)
  1122. /* setup leden_gpio */
  1123. ret = cm36686_setup_leden_gpio(cm36686);
  1124. if (ret) {
  1125. pr_err("%s: could not setup leden_gpio\n", __func__);
  1126. goto err_setup_leden_gpio;
  1127. }
  1128. cm36686_leden_gpio_onoff(cm36686, 1);
  1129. #else
  1130. prox_led_onoff(cm36686, 1);
  1131. #endif
  1132. /* Check if the device is there or not. */
  1133. ret = cm36686_i2c_write_word(cm36686, REG_CS_CONF1, 0x0001);
  1134. if (ret < 0) {
  1135. pr_err("[SENSOR] %s: cm36686 is not connected.(%d)\n", __func__,
  1136. ret);
  1137. goto err_setup_reg;
  1138. }
  1139. /* setup initial registers */
  1140. ret = cm36686_setup_reg(cm36686);
  1141. if (ret < 0) {
  1142. pr_err("[SENSOR] %s: could not setup regs\n", __func__);
  1143. goto err_setup_reg;
  1144. }
  1145. #if defined(CONFIG_SENSORS_CM36686_LEDA_EN_GPIO)
  1146. cm36686_leden_gpio_onoff(cm36686, 0);
  1147. #else
  1148. prox_led_onoff(cm36686, 0);
  1149. #endif
  1150. /* allocate proximity input_device */
  1151. cm36686->proximity_input_dev = input_allocate_device();
  1152. if (!cm36686->proximity_input_dev) {
  1153. pr_err("%s: could not allocate proximity input device\n",
  1154. __func__);
  1155. goto err_input_allocate_device_proximity;
  1156. }
  1157. input_set_drvdata(cm36686->proximity_input_dev, cm36686);
  1158. cm36686->proximity_input_dev->name = "proximity_sensor";
  1159. input_set_capability(cm36686->proximity_input_dev, EV_ABS,
  1160. ABS_DISTANCE);
  1161. input_set_abs_params(cm36686->proximity_input_dev, ABS_DISTANCE, 0, 1,
  1162. 0, 0);
  1163. ret = input_register_device(cm36686->proximity_input_dev);
  1164. if (ret < 0) {
  1165. input_free_device(cm36686->proximity_input_dev);
  1166. pr_err("[SENSOR] %s: could not register input device\n",
  1167. __func__);
  1168. goto err_input_register_device_proximity;
  1169. }
  1170. ret = sensors_create_symlink(&cm36686->proximity_input_dev->dev.kobj,
  1171. cm36686->proximity_input_dev->name);
  1172. if (ret < 0) {
  1173. pr_err("[SENSOR] %s: create_symlink error\n", __func__);
  1174. goto err_sensors_create_symlink_prox;
  1175. }
  1176. ret = sysfs_create_group(&cm36686->proximity_input_dev->dev.kobj,
  1177. &proximity_attribute_group);
  1178. if (ret) {
  1179. pr_err("[SENSOR] %s: could not create sysfs group\n", __func__);
  1180. goto err_sysfs_create_group_proximity;
  1181. }
  1182. /* setup irq */
  1183. ret = cm36686_setup_irq(cm36686);
  1184. if (ret) {
  1185. pr_err("%s: could not setup irq\n", __func__);
  1186. goto err_setup_irq;
  1187. }
  1188. /* For factory test mode, we use timer to get average proximity data. */
  1189. /* prox_timer settings. we poll for light values using a timer. */
  1190. hrtimer_init(&cm36686->prox_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1191. cm36686->prox_poll_delay = ns_to_ktime(2000 * NSEC_PER_MSEC);/*2 sec*/
  1192. cm36686->prox_timer.function = cm36686_prox_timer_func;
  1193. /* the timer just fires off a work queue request. we need a thread
  1194. to read the i2c (can be slow and blocking). */
  1195. cm36686->prox_wq = create_singlethread_workqueue("cm36686_prox_wq");
  1196. if (!cm36686->prox_wq) {
  1197. ret = -ENOMEM;
  1198. pr_err("[SENSOR] %s: could not create prox workqueue\n",
  1199. __func__);
  1200. goto err_create_prox_workqueue;
  1201. }
  1202. /* this is the thread function we run on the work queue */
  1203. INIT_WORK(&cm36686->work_prox, cm36686_work_func_prox);
  1204. /* allocate lightsensor input_device */
  1205. cm36686->light_input_dev = input_allocate_device();
  1206. if (!cm36686->light_input_dev) {
  1207. pr_err("%s: could not allocate light input device\n", __func__);
  1208. goto err_input_allocate_device_light;
  1209. }
  1210. input_set_drvdata(cm36686->light_input_dev, cm36686);
  1211. cm36686->light_input_dev->name = "light_sensor";
  1212. input_set_capability(cm36686->light_input_dev, EV_REL, REL_MISC);
  1213. input_set_capability(cm36686->light_input_dev, EV_REL, REL_DIAL);
  1214. input_set_capability(cm36686->light_input_dev, EV_REL, REL_WHEEL);
  1215. ret = input_register_device(cm36686->light_input_dev);
  1216. if (ret < 0) {
  1217. input_free_device(cm36686->light_input_dev);
  1218. pr_err("%s: could not register input device\n", __func__);
  1219. goto err_input_register_device_light;
  1220. }
  1221. ret = sensors_create_symlink(&cm36686->light_input_dev->dev.kobj,
  1222. cm36686->light_input_dev->name);
  1223. if (ret < 0) {
  1224. pr_err("[SENSOR] %s: create_symlink error\n", __func__);
  1225. goto err_sensors_create_symlink_light;
  1226. }
  1227. ret = sysfs_create_group(&cm36686->light_input_dev->dev.kobj,
  1228. &light_attribute_group);
  1229. if (ret) {
  1230. pr_err("[SENSOR] %s: could not create sysfs group\n", __func__);
  1231. goto err_sysfs_create_group_light;
  1232. }
  1233. /* light_timer settings. we poll for light values using a timer. */
  1234. hrtimer_init(&cm36686->light_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1235. cm36686->light_poll_delay = ns_to_ktime(200 * NSEC_PER_MSEC);
  1236. cm36686->light_timer.function = cm36686_light_timer_func;
  1237. /* the timer just fires off a work queue request. we need a thread
  1238. to read the i2c (can be slow and blocking). */
  1239. cm36686->light_wq = create_singlethread_workqueue("cm36686_light_wq");
  1240. if (!cm36686->light_wq) {
  1241. ret = -ENOMEM;
  1242. pr_err("[SENSOR] %s: could not create light workqueue\n",
  1243. __func__);
  1244. goto err_create_light_workqueue;
  1245. }
  1246. /* this is the thread function we run on the work queue */
  1247. INIT_WORK(&cm36686->work_light, cm36686_work_func_light);
  1248. /* set sysfs for proximity sensor */
  1249. ret = sensors_register(cm36686->proximity_dev,
  1250. cm36686, prox_sensor_attrs, "proximity_sensor");
  1251. if (ret) {
  1252. pr_err("[SENSOR] %s: could not create proximity_dev ret is %d\n",
  1253. __func__, ret);
  1254. goto prox_sensor_register_failed;
  1255. }
  1256. /* set sysfs for light sensor */
  1257. ret = sensors_register(cm36686->light_dev,
  1258. cm36686, light_sensor_attrs, "light_sensor");
  1259. if (ret) {
  1260. pr_err("[SENSOR] %s: could not create light_dev ret is %d\n",
  1261. __func__, ret);
  1262. goto light_sensor_register_failed;
  1263. }
  1264. pr_info("[SENSOR] %s is success.\n", __func__);
  1265. goto done;
  1266. err_devicetree:
  1267. pr_err("[SENSOR] error in device tree");
  1268. /* error, unwind it all */
  1269. light_sensor_register_failed:
  1270. sensors_unregister(cm36686->proximity_dev, prox_sensor_attrs);
  1271. prox_sensor_register_failed:
  1272. destroy_workqueue(cm36686->light_wq);
  1273. err_create_light_workqueue:
  1274. sysfs_remove_group(&cm36686->light_input_dev->dev.kobj,
  1275. &light_attribute_group);
  1276. err_sysfs_create_group_light:
  1277. sensors_remove_symlink(&cm36686->light_input_dev->dev.kobj,
  1278. cm36686->light_input_dev->name);
  1279. err_sensors_create_symlink_light:
  1280. input_unregister_device(cm36686->light_input_dev);
  1281. err_input_register_device_light:
  1282. err_input_allocate_device_light:
  1283. destroy_workqueue(cm36686->prox_wq);
  1284. err_create_prox_workqueue:
  1285. free_irq(cm36686->irq, cm36686);
  1286. gpio_free(cm36686->pdata->irq);
  1287. err_setup_irq:
  1288. sysfs_remove_group(&cm36686->proximity_input_dev->dev.kobj,
  1289. &proximity_attribute_group);
  1290. err_sysfs_create_group_proximity:
  1291. sensors_remove_symlink(&cm36686->proximity_input_dev->dev.kobj,
  1292. cm36686->proximity_input_dev->name);
  1293. err_sensors_create_symlink_prox:
  1294. input_unregister_device(cm36686->proximity_input_dev);
  1295. err_input_register_device_proximity:
  1296. err_input_allocate_device_proximity:
  1297. err_setup_reg:
  1298. #if defined(CONFIG_SENSORS_CM36686_LEDA_EN_GPIO)
  1299. cm36686_leden_gpio_onoff(cm36686, 0);
  1300. gpio_free(cm36686->pdata->leden_gpio);
  1301. err_setup_leden_gpio:
  1302. #else
  1303. prox_led_onoff(cm36686, 0);
  1304. #endif
  1305. wake_lock_destroy(&cm36686->prx_wake_lock);
  1306. mutex_destroy(&cm36686->read_lock);
  1307. mutex_destroy(&cm36686->power_lock);
  1308. kfree(cm36686);
  1309. prox_regulator_onoff(&client->dev, 0);
  1310. done:
  1311. return ret;
  1312. }
  1313. static int cm36686_i2c_remove(struct i2c_client *client)
  1314. {
  1315. struct cm36686_data *cm36686 = i2c_get_clientdata(client);
  1316. /* free irq */
  1317. if (cm36686->power_state & PROXIMITY_ENABLED) {
  1318. disable_irq_wake(cm36686->irq);
  1319. disable_irq(cm36686->irq);
  1320. }
  1321. free_irq(cm36686->irq, cm36686);
  1322. gpio_free(cm36686->pdata->irq);
  1323. /* device off */
  1324. if (cm36686->power_state & LIGHT_ENABLED)
  1325. cm36686_light_disable(cm36686);
  1326. if (cm36686->power_state & PROXIMITY_ENABLED) {
  1327. cm36686_i2c_write_word(cm36686, REG_PS_CONF1,
  1328. 0x0001);
  1329. }
  1330. /* destroy workqueue */
  1331. destroy_workqueue(cm36686->light_wq);
  1332. destroy_workqueue(cm36686->prox_wq);
  1333. /* sysfs destroy */
  1334. sensors_unregister(cm36686->light_dev, light_sensor_attrs);
  1335. sensors_unregister(cm36686->proximity_dev, prox_sensor_attrs);
  1336. sensors_remove_symlink(&cm36686->light_input_dev->dev.kobj,
  1337. cm36686->light_input_dev->name);
  1338. sensors_remove_symlink(&cm36686->proximity_input_dev->dev.kobj,
  1339. cm36686->proximity_input_dev->name);
  1340. /* input device destroy */
  1341. sysfs_remove_group(&cm36686->light_input_dev->dev.kobj,
  1342. &light_attribute_group);
  1343. input_unregister_device(cm36686->light_input_dev);
  1344. sysfs_remove_group(&cm36686->proximity_input_dev->dev.kobj,
  1345. &proximity_attribute_group);
  1346. input_unregister_device(cm36686->proximity_input_dev);
  1347. #if defined(CONFIG_SENSORS_CM36686_LEDA_EN_GPIO)
  1348. cm36686_leden_gpio_onoff(cm36686, 0);
  1349. gpio_free(cm36686->pdata->leden_gpio);
  1350. #else
  1351. prox_led_onoff(cm36686, 0);
  1352. #endif
  1353. /* lock destroy */
  1354. mutex_destroy(&cm36686->read_lock);
  1355. mutex_destroy(&cm36686->power_lock);
  1356. wake_lock_destroy(&cm36686->prx_wake_lock);
  1357. kfree(cm36686);
  1358. return 0;
  1359. }
  1360. static int cm36686_suspend(struct device *dev)
  1361. {
  1362. /* We disable power only if proximity is disabled. If proximity
  1363. is enabled, we leave power on because proximity is allowed
  1364. to wake up device. We remove power without changing
  1365. cm36686->power_state because we use that state in resume.
  1366. */
  1367. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  1368. if (cm36686->power_state & LIGHT_ENABLED)
  1369. cm36686_light_disable(cm36686);
  1370. return 0;
  1371. }
  1372. static int cm36686_resume(struct device *dev)
  1373. {
  1374. struct cm36686_data *cm36686 = dev_get_drvdata(dev);
  1375. if (cm36686->power_state & LIGHT_ENABLED)
  1376. cm36686_light_enable(cm36686);
  1377. return 0;
  1378. }
  1379. #ifdef CONFIG_OF
  1380. static struct of_device_id cm36686_match_table[] = {
  1381. { .compatible = "cm36686",},
  1382. {},
  1383. };
  1384. #else
  1385. #define cm36686_match_table NULL
  1386. #endif
  1387. static const struct i2c_device_id cm36686_device_id[] = {
  1388. {"cm36686", 0},
  1389. {}
  1390. };
  1391. MODULE_DEVICE_TABLE(i2c, cm36686_device_id);
  1392. static const struct dev_pm_ops cm36686_pm_ops = {
  1393. .suspend = cm36686_suspend,
  1394. .resume = cm36686_resume
  1395. };
  1396. static struct i2c_driver cm36686_i2c_driver = {
  1397. .driver = {
  1398. .name = "cm36686",
  1399. .owner = THIS_MODULE,
  1400. .of_match_table = cm36686_match_table,
  1401. .pm = &cm36686_pm_ops
  1402. },
  1403. .probe = cm36686_i2c_probe,
  1404. .remove = cm36686_i2c_remove,
  1405. .id_table = cm36686_device_id,
  1406. };
  1407. static int __init cm36686_init(void)
  1408. {
  1409. return i2c_add_driver(&cm36686_i2c_driver);
  1410. }
  1411. static void __exit cm36686_exit(void)
  1412. {
  1413. i2c_del_driver(&cm36686_i2c_driver);
  1414. }
  1415. module_init(cm36686_init);
  1416. module_exit(cm36686_exit);
  1417. MODULE_AUTHOR("Samsung Electronics");
  1418. MODULE_DESCRIPTION("RGB Sensor device driver for cm36686");
  1419. MODULE_LICENSE("GPL");