cm36283.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  1. /* drivers/input/misc/cm36283.c - cm36283 optical sensors driver
  2. *
  3. * Copyright (C) 2012 Capella Microsystems Inc.
  4. * Author: Frank Hsieh <pengyueh@gmail.com>
  5. *
  6. * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/delay.h>
  19. #include <linux/i2c.h>
  20. #include <linux/input.h>
  21. #include <linux/sensors.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/irq.h>
  27. #include <linux/errno.h>
  28. #include <linux/err.h>
  29. #include <linux/gpio.h>
  30. #include <linux/miscdevice.h>
  31. #include <linux/slab.h>
  32. #include <linux/regulator/consumer.h>
  33. #include <linux/wakelock.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/cm36283.h>
  36. #include <linux/of_gpio.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/mach-types.h>
  39. #include <asm/setup.h>
  40. #define I2C_RETRY_COUNT 10
  41. #define NEAR_DELAY_TIME ((100 * HZ) / 1000)
  42. #define CONTROL_INT_ISR_REPORT 0x00
  43. #define CONTROL_ALS 0x01
  44. #define CONTROL_PS 0x02
  45. /* POWER SUPPLY VOLTAGE RANGE */
  46. #define CM36283_VDD_MIN_UV 2700000
  47. #define CM36283_VDD_MAX_UV 3300000
  48. #define CM36283_VI2C_MIN_UV 1750000
  49. #define CM36283_VI2C_MAX_UV 1950000
  50. /* cm36283 polling rate in ms */
  51. #define CM36283_LS_MIN_POLL_DELAY 1
  52. #define CM36283_LS_MAX_POLL_DELAY 1000
  53. #define CM36283_LS_DEFAULT_POLL_DELAY 100
  54. #define CM36283_PS_MIN_POLL_DELAY 1
  55. #define CM36283_PS_MAX_POLL_DELAY 1000
  56. #define CM36283_PS_DEFAULT_POLL_DELAY 100
  57. static struct sensors_classdev sensors_light_cdev = {
  58. .name = "cm36283-light",
  59. .vendor = "Capella",
  60. .version = 1,
  61. .handle = SENSORS_LIGHT_HANDLE,
  62. .type = SENSOR_TYPE_LIGHT,
  63. .max_range = "6553",
  64. .resolution = "0.0125",
  65. .sensor_power = "0.15",
  66. .min_delay = 0,
  67. .fifo_reserved_event_count = 0,
  68. .fifo_max_event_count = 0,
  69. .enabled = 0,
  70. .delay_msec = CM36283_LS_DEFAULT_POLL_DELAY,
  71. .sensors_enable = NULL,
  72. .sensors_poll_delay = NULL,
  73. };
  74. static struct sensors_classdev sensors_proximity_cdev = {
  75. .name = "cm36283-proximity",
  76. .vendor = "Capella",
  77. .version = 1,
  78. .handle = SENSORS_PROXIMITY_HANDLE,
  79. .type = SENSOR_TYPE_PROXIMITY,
  80. .max_range = "5.0",
  81. .resolution = "5.0",
  82. .sensor_power = "0.18",
  83. .min_delay = 0,
  84. .fifo_reserved_event_count = 0,
  85. .fifo_max_event_count = 0,
  86. .enabled = 0,
  87. .delay_msec = CM36283_PS_DEFAULT_POLL_DELAY,
  88. .sensors_enable = NULL,
  89. .sensors_poll_delay = NULL,
  90. };
  91. static const int als_range[] = {
  92. [CM36283_ALS_IT0] = 6554,
  93. [CM36283_ALS_IT1] = 3277,
  94. [CM36283_ALS_IT2] = 1638,
  95. [CM36283_ALS_IT3] = 819,
  96. };
  97. static const int als_sense[] = {
  98. [CM36283_ALS_IT0] = 10,
  99. [CM36283_ALS_IT1] = 20,
  100. [CM36283_ALS_IT2] = 40,
  101. [CM36283_ALS_IT3] = 80,
  102. };
  103. static void sensor_irq_do_work(struct work_struct *work);
  104. static DECLARE_WORK(sensor_irq_work, sensor_irq_do_work);
  105. struct cm36283_info {
  106. struct class *cm36283_class;
  107. struct device *ls_dev;
  108. struct device *ps_dev;
  109. struct input_dev *ls_input_dev;
  110. struct input_dev *ps_input_dev;
  111. struct i2c_client *i2c_client;
  112. struct workqueue_struct *lp_wq;
  113. int intr_pin;
  114. int als_enable;
  115. int ps_enable;
  116. int ps_irq_flag;
  117. uint16_t *adc_table;
  118. uint16_t cali_table[10];
  119. int irq;
  120. int ls_calibrate;
  121. int (*power)(int, uint8_t); /* power to the chip */
  122. uint32_t als_kadc;
  123. uint32_t als_gadc;
  124. uint16_t golden_adc;
  125. struct wake_lock ps_wake_lock;
  126. int psensor_opened;
  127. int lightsensor_opened;
  128. uint8_t slave_addr;
  129. uint8_t ps_close_thd_set;
  130. uint8_t ps_away_thd_set;
  131. int current_level;
  132. uint16_t current_adc;
  133. uint16_t ps_conf1_val;
  134. uint16_t ps_conf3_val;
  135. uint16_t ls_cmd;
  136. uint8_t record_clear_int_fail;
  137. bool polling;
  138. atomic_t ls_poll_delay;
  139. atomic_t ps_poll_delay;
  140. struct regulator *vdd;
  141. struct regulator *vio;
  142. struct delayed_work ldwork;
  143. struct delayed_work pdwork;
  144. struct sensors_classdev als_cdev;
  145. struct sensors_classdev ps_cdev;
  146. };
  147. struct cm36283_info *lp_info;
  148. int fLevel=-1;
  149. static struct mutex als_enable_mutex, als_disable_mutex, als_get_adc_mutex;
  150. static struct mutex ps_enable_mutex, ps_disable_mutex, ps_get_adc_mutex;
  151. static struct mutex CM36283_control_mutex;
  152. static struct mutex wq_lock;
  153. static int lightsensor_enable(struct cm36283_info *lpi);
  154. static int lightsensor_disable(struct cm36283_info *lpi);
  155. static int initial_cm36283(struct cm36283_info *lpi);
  156. static void psensor_initial_cmd(struct cm36283_info *lpi);
  157. static int cm36283_power_set(struct cm36283_info *info, bool on);
  158. int32_t als_kadc;
  159. static int control_and_report(struct cm36283_info *lpi, uint8_t mode,
  160. uint16_t param, int report);
  161. static int I2C_RxData(uint16_t slaveAddr, uint8_t cmd, uint8_t *rxData, int length)
  162. {
  163. uint8_t loop_i;
  164. struct cm36283_info *lpi = lp_info;
  165. uint8_t subaddr[1];
  166. struct i2c_msg msgs[] = {
  167. {
  168. .addr = slaveAddr,
  169. .flags = 0,
  170. .len = 1,
  171. .buf = subaddr,
  172. },
  173. {
  174. .addr = slaveAddr,
  175. .flags = I2C_M_RD,
  176. .len = length,
  177. .buf = rxData,
  178. },
  179. };
  180. subaddr[0] = cmd;
  181. for (loop_i = 0; loop_i < I2C_RETRY_COUNT; loop_i++) {
  182. if (i2c_transfer(lp_info->i2c_client->adapter, msgs, 2) > 0)
  183. break;
  184. dev_err(&lpi->i2c_client->dev, "%s: I2C error(%d). Retrying.\n",
  185. __func__, cmd);
  186. msleep(10);
  187. }
  188. if (loop_i >= I2C_RETRY_COUNT) {
  189. dev_err(&lpi->i2c_client->dev, "%s: Retry count exceeds %d.",
  190. __func__, I2C_RETRY_COUNT);
  191. return -EIO;
  192. }
  193. return 0;
  194. }
  195. static int I2C_TxData(uint16_t slaveAddr, uint8_t *txData, int length)
  196. {
  197. uint8_t loop_i;
  198. struct cm36283_info *lpi = lp_info;
  199. struct i2c_msg msg[] = {
  200. {
  201. .addr = slaveAddr,
  202. .flags = 0,
  203. .len = length,
  204. .buf = txData,
  205. },
  206. };
  207. for (loop_i = 0; loop_i < I2C_RETRY_COUNT; loop_i++) {
  208. if (i2c_transfer(lp_info->i2c_client->adapter, msg, 1) > 0)
  209. break;
  210. pr_err("%s: I2C error. Retrying...\n", __func__);
  211. msleep(10);
  212. }
  213. if (loop_i >= I2C_RETRY_COUNT) {
  214. dev_err(&lpi->i2c_client->dev, "%s: Retry count exceeds %d.",
  215. __func__, I2C_RETRY_COUNT);
  216. return -EIO;
  217. }
  218. return 0;
  219. }
  220. static int _cm36283_I2C_Read_Word(uint16_t slaveAddr, uint8_t cmd, uint16_t *pdata)
  221. {
  222. uint8_t buffer[2];
  223. int ret = 0;
  224. if (pdata == NULL)
  225. return -EFAULT;
  226. ret = I2C_RxData(slaveAddr, cmd, buffer, 2);
  227. if (ret < 0) {
  228. pr_err("%s: I2C RxData fail(%d).\n", __func__, cmd);
  229. return ret;
  230. }
  231. *pdata = (buffer[1]<<8)|buffer[0];
  232. return ret;
  233. }
  234. static int _cm36283_I2C_Write_Word(uint16_t SlaveAddress, uint8_t cmd, uint16_t data)
  235. {
  236. char buffer[3];
  237. int ret = 0;
  238. buffer[0] = cmd;
  239. buffer[1] = (uint8_t)(data&0xff);
  240. buffer[2] = (uint8_t)((data&0xff00)>>8);
  241. ret = I2C_TxData(SlaveAddress, buffer, 3);
  242. if (ret < 0) {
  243. pr_err("%s: I2C_TxData failed.\n", __func__);
  244. return -EIO;
  245. }
  246. return ret;
  247. }
  248. static int get_ls_adc_value(uint16_t *als_step, bool resume)
  249. {
  250. struct cm36283_info *lpi = lp_info;
  251. uint32_t tmp;
  252. int ret = 0;
  253. if (als_step == NULL)
  254. return -EFAULT;
  255. /* Read ALS data: */
  256. ret = _cm36283_I2C_Read_Word(lpi->slave_addr, ALS_DATA, als_step);
  257. if (ret < 0) {
  258. dev_err(&lpi->i2c_client->dev, "%s: I2C read word failed.\n",
  259. __func__);
  260. return -EIO;
  261. }
  262. if (!lpi->ls_calibrate) {
  263. tmp = (uint32_t)(*als_step) * lpi->als_gadc / lpi->als_kadc;
  264. if (tmp > 0xFFFF)
  265. *als_step = 0xFFFF;
  266. else
  267. *als_step = tmp;
  268. }
  269. dev_dbg(&lpi->i2c_client->dev, "raw adc = 0x%x\n", *als_step);
  270. return ret;
  271. }
  272. static int set_lsensor_range(uint16_t low_thd, uint16_t high_thd)
  273. {
  274. int ret = 0;
  275. struct cm36283_info *lpi = lp_info;
  276. _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_THDH, high_thd);
  277. _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_THDL, low_thd);
  278. return ret;
  279. }
  280. static int get_ps_adc_value(uint16_t *data)
  281. {
  282. int ret = 0;
  283. struct cm36283_info *lpi = lp_info;
  284. if (data == NULL)
  285. return -EFAULT;
  286. ret = _cm36283_I2C_Read_Word(lpi->slave_addr, PS_DATA, data);
  287. if (ret < 0)
  288. return ret;
  289. (*data) &= 0xFF;
  290. return ret;
  291. }
  292. static uint16_t mid_value(uint16_t value[], uint8_t size)
  293. {
  294. int i = 0, j = 0;
  295. uint16_t temp = 0;
  296. if (size < 3)
  297. return 0;
  298. for (i = 0; i < (size - 1); i++)
  299. for (j = (i + 1); j < size; j++)
  300. if (value[i] > value[j]) {
  301. temp = value[i];
  302. value[i] = value[j];
  303. value[j] = temp;
  304. }
  305. return value[((size - 1) / 2)];
  306. }
  307. static int get_stable_ps_adc_value(uint16_t *ps_adc)
  308. {
  309. uint16_t value[3] = {0, 0, 0}, mid_val = 0;
  310. int ret = 0;
  311. int i = 0;
  312. int wait_count = 0;
  313. struct cm36283_info *lpi = lp_info;
  314. for (i = 0; i < 3; i++) {
  315. /*wait interrupt GPIO high*/
  316. while (gpio_get_value(lpi->intr_pin) == 0) {
  317. msleep(10);
  318. wait_count++;
  319. if (wait_count > 12) {
  320. dev_err(&lpi->i2c_client->dev, "%s: interrupt GPIO low\n",
  321. __func__);
  322. return -EIO;
  323. }
  324. }
  325. ret = get_ps_adc_value(&value[i]);
  326. if (ret < 0) {
  327. dev_err(&lpi->i2c_client->dev,
  328. "%s: error get ps value\n", __func__);
  329. return -EIO;
  330. }
  331. if (wait_count < 60/10) {/*wait gpio less than 60ms*/
  332. msleep(60 - (10*wait_count));
  333. }
  334. wait_count = 0;
  335. }
  336. mid_val = mid_value(value, 3);
  337. dev_dbg(&lpi->i2c_client->dev, "Sta_ps: After sort, value[0, 1, 2] = [0x%x, 0x%x, 0x%x]",
  338. value[0], value[1], value[2]);
  339. *ps_adc = (mid_val & 0xFF);
  340. return 0;
  341. }
  342. static void sensor_irq_do_work(struct work_struct *work)
  343. {
  344. struct cm36283_info *lpi = lp_info;
  345. uint16_t intFlag;
  346. _cm36283_I2C_Read_Word(lpi->slave_addr, INT_FLAG, &intFlag);
  347. control_and_report(lpi, CONTROL_INT_ISR_REPORT, intFlag, 1);
  348. enable_irq(lpi->irq);
  349. }
  350. static int get_als_range(void)
  351. {
  352. uint16_t ls_conf;
  353. int ret = 0;
  354. int index = 0;
  355. struct cm36283_info *lpi = lp_info;
  356. ret = _cm36283_I2C_Read_Word(lpi->slave_addr, ALS_CONF, &ls_conf);
  357. if (ret) {
  358. dev_err(&lpi->i2c_client->dev, "read ALS_CONF from i2c error. %d\n",
  359. ret);
  360. return -EIO;
  361. }
  362. index = (ls_conf & 0xC0) >> 0x06;
  363. return als_range[index];
  364. }
  365. static int get_als_sense(void)
  366. {
  367. uint16_t ls_conf;
  368. int ret = 0;
  369. int index = 0;
  370. struct cm36283_info *lpi = lp_info;
  371. ret = _cm36283_I2C_Read_Word(lpi->slave_addr, ALS_CONF, &ls_conf);
  372. if (ret) {
  373. dev_err(&lpi->i2c_client->dev, "read ALS_CONF from i2c error. %d\n",
  374. ret);
  375. return -EIO;
  376. }
  377. index = (ls_conf & 0xC0) >> 0x06;
  378. return als_sense[index];
  379. }
  380. static void psensor_delay_work_handler(struct work_struct *work)
  381. {
  382. struct cm36283_info *lpi = lp_info;
  383. uint16_t adc_value = 0;
  384. int ret;
  385. mutex_lock(&wq_lock);
  386. ret = get_ps_adc_value(&adc_value);
  387. mutex_unlock(&wq_lock);
  388. if (ret >= 0) {
  389. input_report_abs(lpi->ps_input_dev, ABS_DISTANCE,
  390. adc_value > lpi->ps_close_thd_set ? 0 : 1);
  391. input_sync(lpi->ps_input_dev);
  392. }
  393. schedule_delayed_work(&lpi->pdwork,
  394. msecs_to_jiffies(atomic_read(&lpi->ps_poll_delay)));
  395. }
  396. static void lsensor_delay_work_handler(struct work_struct *work)
  397. {
  398. struct cm36283_info *lpi = lp_info;
  399. uint16_t adc_value = 0;
  400. int sense;
  401. mutex_lock(&wq_lock);
  402. get_ls_adc_value(&adc_value, 0);
  403. sense = get_als_sense();
  404. mutex_unlock(&wq_lock);
  405. if (sense > 0) {
  406. lpi->current_adc = adc_value;
  407. input_report_abs(lpi->ls_input_dev, ABS_MISC, adc_value/sense);
  408. input_sync(lpi->ls_input_dev);
  409. }
  410. schedule_delayed_work(&lpi->ldwork,
  411. msecs_to_jiffies(atomic_read(&lpi->ls_poll_delay)));
  412. }
  413. static irqreturn_t cm36283_irq_handler(int irq, void *data)
  414. {
  415. struct cm36283_info *lpi = data;
  416. disable_irq_nosync(lpi->irq);
  417. queue_work(lpi->lp_wq, &sensor_irq_work);
  418. return IRQ_HANDLED;
  419. }
  420. static int als_power(int enable)
  421. {
  422. struct cm36283_info *lpi = lp_info;
  423. if (lpi->power)
  424. lpi->power(LS_PWR_ON, 1);
  425. return 0;
  426. }
  427. static void ls_initial_cmd(struct cm36283_info *lpi)
  428. {
  429. /*must disable l-sensor interrupt befrore IST create*//*disable ALS func*/
  430. lpi->ls_cmd &= CM36283_ALS_INT_MASK;
  431. lpi->ls_cmd |= CM36283_ALS_SD;
  432. _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd);
  433. }
  434. static void psensor_initial_cmd(struct cm36283_info *lpi)
  435. {
  436. /*must disable p-sensor interrupt befrore IST create*/
  437. lpi->ps_conf1_val |= CM36283_PS_SD;
  438. lpi->ps_conf1_val &= CM36283_PS_INT_MASK;
  439. _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF1, lpi->ps_conf1_val);
  440. _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF3, lpi->ps_conf3_val);
  441. _cm36283_I2C_Write_Word(lpi->slave_addr, PS_THD,
  442. (lpi->ps_close_thd_set << 8) | lpi->ps_away_thd_set);
  443. dev_dbg(&lpi->i2c_client->dev,
  444. "%s:send psensor initial command finished\n", __func__);
  445. }
  446. static int psensor_enable(struct cm36283_info *lpi)
  447. {
  448. int ret = -EIO;
  449. unsigned int delay;
  450. mutex_lock(&ps_enable_mutex);
  451. dev_dbg(&lpi->i2c_client->dev, "psensor enable!\n");
  452. if (lpi->ps_enable) {
  453. dev_err(&lpi->i2c_client->dev, "already enabled\n");
  454. ret = 0;
  455. } else {
  456. ret = control_and_report(lpi, CONTROL_PS, 1, 0);
  457. }
  458. mutex_unlock(&ps_enable_mutex);
  459. delay = atomic_read(&lpi->ps_poll_delay);
  460. if (lpi->polling)
  461. schedule_delayed_work(&lpi->pdwork, msecs_to_jiffies(delay));
  462. return ret;
  463. }
  464. static int psensor_disable(struct cm36283_info *lpi)
  465. {
  466. int ret = -EIO;
  467. if (lpi->polling)
  468. cancel_delayed_work_sync(&lpi->pdwork);
  469. mutex_lock(&ps_disable_mutex);
  470. dev_dbg(&lpi->i2c_client->dev, "psensor disable!\n");
  471. if (lpi->ps_enable == 0) {
  472. dev_err(&lpi->i2c_client->dev, "already disabled\n");
  473. ret = 0;
  474. } else {
  475. ret = control_and_report(lpi, CONTROL_PS, 0, 0);
  476. }
  477. mutex_unlock(&ps_disable_mutex);
  478. return ret;
  479. }
  480. static int psensor_open(struct inode *inode, struct file *file)
  481. {
  482. struct cm36283_info *lpi = lp_info;
  483. dev_dbg(&lpi->i2c_client->dev, "psensor open!");
  484. if (lpi->psensor_opened)
  485. return -EBUSY;
  486. lpi->psensor_opened = 1;
  487. return 0;
  488. }
  489. static int psensor_release(struct inode *inode, struct file *file)
  490. {
  491. struct cm36283_info *lpi = lp_info;
  492. dev_dbg(&lpi->i2c_client->dev, "psensor release!");
  493. lpi->psensor_opened = 0;
  494. return psensor_disable(lpi);
  495. //return 0;
  496. }
  497. static long psensor_ioctl(struct file *file, unsigned int cmd,
  498. unsigned long arg)
  499. {
  500. int val;
  501. struct cm36283_info *lpi = lp_info;
  502. dev_dbg(&lpi->i2c_client->dev, "%s cmd %d\n", __func__, _IOC_NR(cmd));
  503. switch (cmd) {
  504. case CAPELLA_CM3602_IOCTL_ENABLE:
  505. if (get_user(val, (unsigned long __user *)arg))
  506. return -EFAULT;
  507. if (val)
  508. return psensor_enable(lpi);
  509. else
  510. return psensor_disable(lpi);
  511. break;
  512. case CAPELLA_CM3602_IOCTL_GET_ENABLED:
  513. return put_user(lpi->ps_enable, (unsigned long __user *)arg);
  514. break;
  515. default:
  516. dev_err(&lpi->i2c_client->dev, "%s: invalid cmd %d\n",
  517. __func__, _IOC_NR(cmd));
  518. return -EINVAL;
  519. }
  520. }
  521. static const struct file_operations psensor_fops = {
  522. .owner = THIS_MODULE,
  523. .open = psensor_open,
  524. .release = psensor_release,
  525. .unlocked_ioctl = psensor_ioctl
  526. };
  527. void lightsensor_set_kvalue(struct cm36283_info *lpi)
  528. {
  529. if (!lpi) {
  530. pr_err("%s: ls_info is empty\n", __func__);
  531. return;
  532. }
  533. dev_dbg(&lpi->i2c_client->dev, "%s: ALS calibrated als_kadc=0x%x\n",
  534. __func__, als_kadc);
  535. if (als_kadc >> 16 == ALS_CALIBRATED)
  536. lpi->als_kadc = als_kadc & 0xFFFF;
  537. else {
  538. lpi->als_kadc = 0;
  539. dev_dbg(&lpi->i2c_client->dev, "%s: no ALS calibrated\n",
  540. __func__);
  541. }
  542. if (lpi->als_kadc && lpi->golden_adc > 0) {
  543. lpi->als_kadc = (lpi->als_kadc > 0 && lpi->als_kadc < 0x1000) ?
  544. lpi->als_kadc : lpi->golden_adc;
  545. lpi->als_gadc = lpi->golden_adc;
  546. } else {
  547. lpi->als_kadc = 1;
  548. lpi->als_gadc = 1;
  549. }
  550. dev_dbg(&lpi->i2c_client->dev, "%s: als_kadc=0x%x, als_gadc=0x%x\n",
  551. __func__, lpi->als_kadc, lpi->als_gadc);
  552. }
  553. static int lightsensor_update_table(struct cm36283_info *lpi)
  554. {
  555. uint32_t tmp_data[10];
  556. int i;
  557. for (i = 0; i < 10; i++) {
  558. tmp_data[i] = (uint32_t)(*(lpi->adc_table + i))
  559. * lpi->als_kadc / lpi->als_gadc;
  560. if (tmp_data[i] <= 0xFFFF)
  561. lpi->cali_table[i] = (uint16_t) tmp_data[i];
  562. else
  563. lpi->cali_table[i] = 0xFFFF;
  564. dev_dbg(&lpi->i2c_client->dev, "%s: Calibrated adc_table: data[%d], %x\n",
  565. __func__, i, lpi->cali_table[i]);
  566. }
  567. return 0;
  568. }
  569. static int lightsensor_enable(struct cm36283_info *lpi)
  570. {
  571. int ret = -EIO;
  572. unsigned int delay;
  573. mutex_lock(&als_enable_mutex);
  574. ret = control_and_report(lpi, CONTROL_ALS, 1, 0);
  575. mutex_unlock(&als_enable_mutex);
  576. delay = atomic_read(&lpi->ls_poll_delay);
  577. if (lpi->polling)
  578. schedule_delayed_work(&lpi->ldwork,
  579. msecs_to_jiffies(delay));
  580. return ret;
  581. }
  582. static int lightsensor_disable(struct cm36283_info *lpi)
  583. {
  584. int ret = -EIO;
  585. mutex_lock(&als_disable_mutex);
  586. dev_dbg(&lpi->i2c_client->dev, "disable lightsensor\n");
  587. if (lpi->polling)
  588. cancel_delayed_work_sync(&lpi->ldwork);
  589. if ( lpi->als_enable == 0 ) {
  590. dev_err(&lpi->i2c_client->dev, "already disabled\n");
  591. ret = 0;
  592. } else {
  593. ret = control_and_report(lpi, CONTROL_ALS, 0, 0);
  594. }
  595. mutex_unlock(&als_disable_mutex);
  596. return ret;
  597. }
  598. static int lightsensor_open(struct inode *inode, struct file *file)
  599. {
  600. struct cm36283_info *lpi = lp_info;
  601. int rc = 0;
  602. dev_dbg(&lpi->i2c_client->dev, "%s\n", __func__);
  603. if (lpi->lightsensor_opened) {
  604. dev_err(&lpi->i2c_client->dev, "%s: already opened\n",
  605. __func__);
  606. rc = -EBUSY;
  607. }
  608. lpi->lightsensor_opened = 1;
  609. return rc;
  610. }
  611. static int lightsensor_release(struct inode *inode, struct file *file)
  612. {
  613. struct cm36283_info *lpi = lp_info;
  614. dev_dbg(&lpi->i2c_client->dev, "%s\n", __func__);
  615. lpi->lightsensor_opened = 0;
  616. return 0;
  617. }
  618. static long lightsensor_ioctl(struct file *file, unsigned int cmd,
  619. unsigned long arg)
  620. {
  621. int rc, val;
  622. struct cm36283_info *lpi = lp_info;
  623. switch (cmd) {
  624. case LIGHTSENSOR_IOCTL_ENABLE:
  625. if (get_user(val, (unsigned long __user *)arg)) {
  626. rc = -EFAULT;
  627. break;
  628. }
  629. rc = val ? lightsensor_enable(lpi) : lightsensor_disable(lpi);
  630. break;
  631. case LIGHTSENSOR_IOCTL_GET_ENABLED:
  632. val = lpi->als_enable;
  633. rc = put_user(val, (unsigned long __user *)arg);
  634. break;
  635. default:
  636. pr_err("[LS][CM36283 error]%s: invalid cmd %d\n",
  637. __func__, _IOC_NR(cmd));
  638. rc = -EINVAL;
  639. }
  640. return rc;
  641. }
  642. static const struct file_operations lightsensor_fops = {
  643. .owner = THIS_MODULE,
  644. .open = lightsensor_open,
  645. .release = lightsensor_release,
  646. .unlocked_ioctl = lightsensor_ioctl
  647. };
  648. static ssize_t ps_adc_show(struct device *dev,
  649. struct device_attribute *attr, char *buf)
  650. {
  651. uint16_t value;
  652. int ret;
  653. struct cm36283_info *lpi = lp_info;
  654. int intr_val = -1;
  655. get_ps_adc_value(&value);
  656. if (gpio_is_valid(lpi->intr_pin))
  657. intr_val = gpio_get_value(lpi->intr_pin);
  658. ret = snprintf(buf, PAGE_SIZE, "ADC[0x%04X], ENABLE=%d intr_pin=%d\n",
  659. value, lpi->ps_enable, intr_val);
  660. return ret;
  661. }
  662. static int ps_enable_set(struct sensors_classdev *sensors_cdev,
  663. unsigned int enable)
  664. {
  665. struct cm36283_info *lpi = container_of(sensors_cdev,
  666. struct cm36283_info, ps_cdev);
  667. int ret;
  668. if (enable)
  669. ret = psensor_enable(lpi);
  670. else
  671. ret = psensor_disable(lpi);
  672. return ret;
  673. }
  674. static ssize_t ps_enable_store(struct device *dev,
  675. struct device_attribute *attr,
  676. const char *buf, size_t count)
  677. {
  678. int ps_en;
  679. struct cm36283_info *lpi = lp_info;
  680. ps_en = -1;
  681. sscanf(buf, "%d", &ps_en);
  682. if (ps_en != 0 && ps_en != 1
  683. && ps_en != 10 && ps_en != 13 && ps_en != 16)
  684. return -EINVAL;
  685. dev_dbg(&lpi->i2c_client->dev, "%s: ps_en=%d\n",
  686. __func__, ps_en);
  687. if (ps_en)
  688. psensor_enable(lpi);
  689. else
  690. psensor_disable(lpi);
  691. return count;
  692. }
  693. static ssize_t ps_parameters_show(struct device *dev,
  694. struct device_attribute *attr, char *buf)
  695. {
  696. int ret;
  697. struct cm36283_info *lpi = lp_info;
  698. ret = snprintf(buf, PAGE_SIZE,
  699. "PS_close_thd_set = 0x%x, PS_away_thd_set = 0x%x\n",
  700. lpi->ps_close_thd_set, lpi->ps_away_thd_set);
  701. return ret;
  702. }
  703. static ssize_t ps_parameters_store(struct device *dev,
  704. struct device_attribute *attr,
  705. const char *buf, size_t count)
  706. {
  707. struct cm36283_info *lpi = lp_info;
  708. char *token[10];
  709. int i;
  710. unsigned long tmp;
  711. for (i = 0; i < 3; i++)
  712. token[i] = strsep((char **)&buf, " ");
  713. if (kstrtoul(token[0], 16, &tmp))
  714. return -EINVAL;
  715. lpi->ps_close_thd_set = tmp;
  716. if (kstrtoul(token[1], 16, &tmp))
  717. return -EINVAL;
  718. lpi->ps_away_thd_set = tmp;
  719. dev_dbg(&lpi->i2c_client->dev, "ps_close_thd_set:0x%x\n",
  720. lpi->ps_close_thd_set);
  721. dev_dbg(&lpi->i2c_client->dev, "ps_away_thd_set:0x%x\n",
  722. lpi->ps_away_thd_set);
  723. return count;
  724. }
  725. static ssize_t ps_conf_show(struct device *dev,
  726. struct device_attribute *attr, char *buf)
  727. {
  728. struct cm36283_info *lpi = lp_info;
  729. return sprintf(buf, "PS_CONF1 = 0x%x, PS_CONF3 = 0x%x\n", lpi->ps_conf1_val, lpi->ps_conf3_val);
  730. }
  731. static ssize_t ps_conf_store(struct device *dev,
  732. struct device_attribute *attr,
  733. const char *buf, size_t count)
  734. {
  735. int code1, code2;
  736. struct cm36283_info *lpi = lp_info;
  737. sscanf(buf, "0x%x 0x%x", &code1, &code2);
  738. dev_dbg(&lpi->i2c_client->dev, "PS_CONF1:0x%x PS_CONF3:0x%x\n",
  739. code1, code2);
  740. lpi->ps_conf1_val = code1;
  741. lpi->ps_conf3_val = code2;
  742. _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF3, lpi->ps_conf3_val);
  743. _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF1, lpi->ps_conf1_val);
  744. return count;
  745. }
  746. static ssize_t ps_thd_show(struct device *dev,
  747. struct device_attribute *attr, char *buf)
  748. {
  749. int ret;
  750. struct cm36283_info *lpi = lp_info;
  751. ret = sprintf(buf, "%s ps_close_thd_set = 0x%x, ps_away_thd_set = 0x%x\n", __func__, lpi->ps_close_thd_set, lpi->ps_away_thd_set);
  752. return ret;
  753. }
  754. static ssize_t ps_thd_store(struct device *dev,
  755. struct device_attribute *attr,
  756. const char *buf, size_t count)
  757. {
  758. int code;
  759. struct cm36283_info *lpi = lp_info;
  760. sscanf(buf, "0x%x", &code);
  761. lpi->ps_away_thd_set = code &0xFF;
  762. lpi->ps_close_thd_set = (code & 0xFF00)>>8;
  763. dev_dbg(&lpi->i2c_client->dev, "ps_away_thd_set:0x%x\n",
  764. lpi->ps_away_thd_set);
  765. dev_dbg(&lpi->i2c_client->dev, "ps_close_thd_set:0x%x\n",
  766. lpi->ps_close_thd_set);
  767. return count;
  768. }
  769. static ssize_t ps_hw_show(struct device *dev,
  770. struct device_attribute *attr, char *buf)
  771. {
  772. int ret = 0;
  773. struct cm36283_info *lpi = lp_info;
  774. ret = sprintf(buf, "PS1: reg = 0x%x, PS3: reg = 0x%x, ps_close_thd_set = 0x%x, ps_away_thd_set = 0x%x\n",
  775. lpi->ps_conf1_val, lpi->ps_conf3_val, lpi->ps_close_thd_set, lpi->ps_away_thd_set);
  776. return ret;
  777. }
  778. static ssize_t ps_hw_store(struct device *dev,
  779. struct device_attribute *attr,
  780. const char *buf, size_t count)
  781. {
  782. int code;
  783. sscanf(buf, "0x%x", &code);
  784. return count;
  785. }
  786. static ssize_t ls_adc_show(struct device *dev,
  787. struct device_attribute *attr, char *buf)
  788. {
  789. int ret;
  790. struct cm36283_info *lpi = lp_info;
  791. ret = sprintf(buf, "ADC[0x%04X] => level %d\n",
  792. lpi->current_adc, lpi->current_level);
  793. return ret;
  794. }
  795. static int ls_enable_set(struct sensors_classdev *sensors_cdev,
  796. unsigned int enable)
  797. {
  798. struct cm36283_info *lpi = container_of(sensors_cdev,
  799. struct cm36283_info, als_cdev);
  800. int ret;
  801. if (enable)
  802. ret = lightsensor_enable(lpi);
  803. else
  804. ret = lightsensor_disable(lpi);
  805. if (ret < 0) {
  806. dev_err(&lpi->i2c_client->dev, "%s: set auto light sensor fail\n",
  807. __func__);
  808. return -EIO;
  809. }
  810. return 0;
  811. }
  812. static ssize_t ls_enable_show(struct device *dev,
  813. struct device_attribute *attr, char *buf)
  814. {
  815. int ret = 0;
  816. struct cm36283_info *lpi = lp_info;
  817. ret = sprintf(buf, "Light sensor Auto Enable = %d\n",
  818. lpi->als_enable);
  819. return ret;
  820. }
  821. static ssize_t ls_enable_store(struct device *dev,
  822. struct device_attribute *attr,
  823. const char *buf, size_t count)
  824. {
  825. int ret = 0;
  826. int ls_auto;
  827. struct cm36283_info *lpi = lp_info;
  828. ls_auto = -1;
  829. sscanf(buf, "%d", &ls_auto);
  830. if (ls_auto != 0 && ls_auto != 1 && ls_auto != 147)
  831. return -EINVAL;
  832. if (ls_auto) {
  833. lpi->ls_calibrate = (ls_auto == 147) ? 1 : 0;
  834. ret = lightsensor_enable(lpi);
  835. } else {
  836. lpi->ls_calibrate = 0;
  837. ret = lightsensor_disable(lpi);
  838. }
  839. dev_dbg(&lpi->i2c_client->dev, "als_enable:0x%x\n",
  840. lpi->als_enable);
  841. dev_dbg(&lpi->i2c_client->dev, "ls_calibrate:0x%x\n",
  842. lpi->ls_calibrate);
  843. dev_dbg(&lpi->i2c_client->dev, "ls_auto:0x%x\n", ls_auto);
  844. if (ret < 0) {
  845. dev_err(&lpi->i2c_client->dev, "%s: set auto light sensor fail\n",
  846. __func__);
  847. return ret;
  848. }
  849. return count;
  850. }
  851. static ssize_t ls_kadc_show(struct device *dev,
  852. struct device_attribute *attr, char *buf)
  853. {
  854. struct cm36283_info *lpi = lp_info;
  855. int ret;
  856. ret = sprintf(buf, "kadc = 0x%x",
  857. lpi->als_kadc);
  858. return ret;
  859. }
  860. static ssize_t ls_kadc_store(struct device *dev,
  861. struct device_attribute *attr,
  862. const char *buf, size_t count)
  863. {
  864. struct cm36283_info *lpi = lp_info;
  865. int kadc_temp = 0;
  866. sscanf(buf, "%d", &kadc_temp);
  867. mutex_lock(&als_get_adc_mutex);
  868. if (kadc_temp != 0) {
  869. lpi->als_kadc = kadc_temp;
  870. if (lpi->als_gadc != 0) {
  871. if (lightsensor_update_table(lpi) < 0)
  872. dev_err(&lpi->i2c_client->dev, "%s: update ls table fail\n",
  873. __func__);
  874. else
  875. dev_dbg(&lpi->i2c_client->dev, "%s: als_gadc =0x%x wait to be set\n",
  876. __func__, lpi->als_gadc);
  877. }
  878. } else {
  879. dev_err(&lpi->i2c_client->dev, "%s: als_kadc can't be set to zero\n",
  880. __func__);
  881. }
  882. mutex_unlock(&als_get_adc_mutex);
  883. return count;
  884. }
  885. static ssize_t ls_gadc_show(struct device *dev,
  886. struct device_attribute *attr, char *buf)
  887. {
  888. struct cm36283_info *lpi = lp_info;
  889. int ret;
  890. ret = sprintf(buf, "gadc = 0x%x\n", lpi->als_gadc);
  891. return ret;
  892. }
  893. static ssize_t ls_gadc_store(struct device *dev,
  894. struct device_attribute *attr,
  895. const char *buf, size_t count)
  896. {
  897. struct cm36283_info *lpi = lp_info;
  898. int gadc_temp = 0;
  899. sscanf(buf, "%d", &gadc_temp);
  900. mutex_lock(&als_get_adc_mutex);
  901. if (gadc_temp != 0) {
  902. lpi->als_gadc = gadc_temp;
  903. if (lpi->als_kadc != 0) {
  904. if (lightsensor_update_table(lpi) < 0)
  905. dev_err(&lpi->i2c_client->dev, "%s: update ls table fail\n",
  906. __func__);
  907. } else {
  908. dev_dbg(&lpi->i2c_client->dev, "als_kadc =0x%x wait to be set\n",
  909. lpi->als_kadc);
  910. }
  911. } else {
  912. dev_err(&lpi->i2c_client->dev, "als_gadc can't be set to zero\n");
  913. }
  914. mutex_unlock(&als_get_adc_mutex);
  915. return count;
  916. }
  917. static ssize_t ls_adc_table_show(struct device *dev,
  918. struct device_attribute *attr, char *buf)
  919. {
  920. unsigned length = 0;
  921. int i;
  922. for (i = 0; i < 10; i++) {
  923. length += sprintf(buf + length,
  924. "[CM36283]Get adc_table[%d] = 0x%x ; %d, Get cali_table[%d] = 0x%x ; %d, \n",
  925. i, *(lp_info->adc_table + i),
  926. *(lp_info->adc_table + i),
  927. i, *(lp_info->cali_table + i),
  928. *(lp_info->cali_table + i));
  929. }
  930. return length;
  931. }
  932. static ssize_t ls_adc_table_store(struct device *dev,
  933. struct device_attribute *attr,
  934. const char *buf, size_t count)
  935. {
  936. struct cm36283_info *lpi = lp_info;
  937. char *token[10];
  938. uint16_t tempdata[10];
  939. int i;
  940. for (i = 0; i < 10; i++) {
  941. token[i] = strsep((char **)&buf, " ");
  942. tempdata[i] = simple_strtoul(token[i], NULL, 16);
  943. if (tempdata[i] < 1 || tempdata[i] > 0xffff) {
  944. dev_err(&lpi->i2c_client->dev,
  945. "adc_table[%d] = 0x%x error\n",
  946. i, tempdata[i]);
  947. return count;
  948. }
  949. }
  950. mutex_lock(&als_get_adc_mutex);
  951. for (i = 0; i < 10; i++)
  952. lpi->adc_table[i] = tempdata[i];
  953. if (lightsensor_update_table(lpi) < 0)
  954. dev_err(&lpi->i2c_client->dev, "%s: update ls table fail\n",
  955. __func__);
  956. mutex_unlock(&als_get_adc_mutex);
  957. return count;
  958. }
  959. static ssize_t ls_conf_show(struct device *dev,
  960. struct device_attribute *attr, char *buf)
  961. {
  962. struct cm36283_info *lpi = lp_info;
  963. return sprintf(buf, "ALS_CONF = %x\n", lpi->ls_cmd);
  964. }
  965. static ssize_t ls_conf_store(struct device *dev,
  966. struct device_attribute *attr,
  967. const char *buf, size_t count)
  968. {
  969. struct cm36283_info *lpi = lp_info;
  970. int value = 0;
  971. sscanf(buf, "0x%x", &value);
  972. lpi->ls_cmd = value;
  973. dev_dbg(&lpi->i2c_client->dev, "ALS_CONF:0x%x\n", lpi->ls_cmd);
  974. _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd);
  975. return count;
  976. }
  977. static ssize_t ls_poll_delay_show(struct device *dev,
  978. struct device_attribute *attr, char *buf)
  979. {
  980. struct cm36283_info *lpi = lp_info;
  981. return snprintf(buf, PAGE_SIZE, "%d\n",
  982. atomic_read(&lpi->ls_poll_delay));
  983. }
  984. static ssize_t ls_poll_delay_store(struct device *dev,
  985. struct device_attribute *attr, const char *buf, size_t count)
  986. {
  987. struct cm36283_info *lpi = lp_info;
  988. unsigned long interval_ms;
  989. if (kstrtoul(buf, 10, &interval_ms))
  990. return -EINVAL;
  991. if ((interval_ms < CM36283_LS_MIN_POLL_DELAY) ||
  992. (interval_ms > CM36283_LS_MAX_POLL_DELAY))
  993. return -EINVAL;
  994. atomic_set(&lpi->ls_poll_delay, (unsigned int) interval_ms);
  995. return count;
  996. }
  997. static int ls_poll_delay_set(struct sensors_classdev *sensors_cdev,
  998. unsigned int delay_msec)
  999. {
  1000. struct cm36283_info *lpi = container_of(sensors_cdev,
  1001. struct cm36283_info, als_cdev);
  1002. if ((delay_msec < CM36283_LS_MIN_POLL_DELAY) ||
  1003. (delay_msec > CM36283_LS_MAX_POLL_DELAY))
  1004. return -EINVAL;
  1005. atomic_set(&lpi->ls_poll_delay, delay_msec);
  1006. return 0;
  1007. }
  1008. static ssize_t ps_poll_delay_show(struct device *dev,
  1009. struct device_attribute *attr, char *buf)
  1010. {
  1011. struct cm36283_info *lpi = lp_info;
  1012. return snprintf(buf, PAGE_SIZE, "%d\n",
  1013. atomic_read(&lpi->ps_poll_delay));
  1014. }
  1015. static ssize_t ps_poll_delay_store(struct device *dev,
  1016. struct device_attribute *attr, const char *buf, size_t count)
  1017. {
  1018. struct cm36283_info *lpi = lp_info;
  1019. unsigned long interval_ms;
  1020. if (kstrtoul(buf, 10, &interval_ms))
  1021. return -EINVAL;
  1022. if ((interval_ms < CM36283_PS_MIN_POLL_DELAY) ||
  1023. (interval_ms > CM36283_PS_MAX_POLL_DELAY))
  1024. return -EINVAL;
  1025. atomic_set(&lpi->ps_poll_delay, (unsigned int) interval_ms);
  1026. return count;
  1027. }
  1028. static int ps_poll_delay_set(struct sensors_classdev *sensors_cdev,
  1029. unsigned int delay_msec)
  1030. {
  1031. struct cm36283_info *lpi = container_of(sensors_cdev,
  1032. struct cm36283_info, als_cdev);
  1033. if ((delay_msec < CM36283_PS_MIN_POLL_DELAY) ||
  1034. (delay_msec > CM36283_PS_MAX_POLL_DELAY))
  1035. return -EINVAL;
  1036. atomic_set(&lpi->ps_poll_delay, delay_msec);
  1037. return 0;
  1038. }
  1039. static ssize_t ls_fLevel_show(struct device *dev,
  1040. struct device_attribute *attr, char *buf)
  1041. {
  1042. return sprintf(buf, "fLevel = %d\n", fLevel);
  1043. }
  1044. static ssize_t ls_fLevel_store(struct device *dev,
  1045. struct device_attribute *attr,
  1046. const char *buf, size_t count)
  1047. {
  1048. struct cm36283_info *lpi = lp_info;
  1049. int value=0;
  1050. sscanf(buf, "%d", &value);
  1051. (value>=0)?(value=min(value,10)):(value=max(value,-1));
  1052. fLevel=value;
  1053. input_report_abs(lpi->ls_input_dev, ABS_MISC, fLevel);
  1054. input_sync(lpi->ls_input_dev);
  1055. msleep(1000);
  1056. fLevel=-1;
  1057. return count;
  1058. }
  1059. static int lightsensor_setup(struct cm36283_info *lpi)
  1060. {
  1061. int ret;
  1062. int range;
  1063. lpi->ls_input_dev = input_allocate_device();
  1064. if (!lpi->ls_input_dev) {
  1065. pr_err(
  1066. "[LS][CM36283 error]%s: could not allocate ls input device\n",
  1067. __func__);
  1068. return -ENOMEM;
  1069. }
  1070. lpi->ls_input_dev->name = "cm36283-ls";
  1071. lpi->ls_input_dev->id.bustype = BUS_I2C;
  1072. set_bit(EV_ABS, lpi->ls_input_dev->evbit);
  1073. range = get_als_range();
  1074. input_set_abs_params(lpi->ls_input_dev, ABS_MISC, 0, range, 0, 0);
  1075. ret = input_register_device(lpi->ls_input_dev);
  1076. if (ret < 0) {
  1077. pr_err("[LS][CM36283 error]%s: can not register ls input device\n",
  1078. __func__);
  1079. goto err_free_ls_input_device;
  1080. }
  1081. return ret;
  1082. err_free_ls_input_device:
  1083. input_free_device(lpi->ls_input_dev);
  1084. return ret;
  1085. }
  1086. static int psensor_setup(struct cm36283_info *lpi)
  1087. {
  1088. int ret;
  1089. lpi->ps_input_dev = input_allocate_device();
  1090. if (!lpi->ps_input_dev) {
  1091. pr_err(
  1092. "[PS][CM36283 error]%s: could not allocate ps input device\n",
  1093. __func__);
  1094. return -ENOMEM;
  1095. }
  1096. lpi->ps_input_dev->name = "cm36283-ps";
  1097. lpi->ps_input_dev->id.bustype = BUS_I2C;
  1098. set_bit(EV_ABS, lpi->ps_input_dev->evbit);
  1099. input_set_abs_params(lpi->ps_input_dev, ABS_DISTANCE, 0, 1, 0, 0);
  1100. ret = input_register_device(lpi->ps_input_dev);
  1101. if (ret < 0) {
  1102. pr_err(
  1103. "[PS][CM36283 error]%s: could not register ps input device\n",
  1104. __func__);
  1105. goto err_free_ps_input_device;
  1106. }
  1107. return ret;
  1108. err_free_ps_input_device:
  1109. input_free_device(lpi->ps_input_dev);
  1110. return ret;
  1111. }
  1112. static int initial_cm36283(struct cm36283_info *lpi)
  1113. {
  1114. int val, ret;
  1115. uint16_t idReg;
  1116. val = gpio_get_value(lpi->intr_pin);
  1117. dev_dbg(&lpi->i2c_client->dev, "%s, INTERRUPT GPIO val = %d\n",
  1118. __func__, val);
  1119. ret = _cm36283_I2C_Read_Word(lpi->slave_addr, ID_REG, &idReg);
  1120. return ret;
  1121. }
  1122. static int cm36283_setup(struct cm36283_info *lpi)
  1123. {
  1124. int ret = 0;
  1125. als_power(1);
  1126. msleep(5);
  1127. ret = gpio_request(lpi->intr_pin, "gpio_cm36283_intr");
  1128. if (ret < 0) {
  1129. pr_err("[PS][CM36283 error]%s: gpio %d request failed (%d)\n",
  1130. __func__, lpi->intr_pin, ret);
  1131. return ret;
  1132. }
  1133. ret = gpio_direction_input(lpi->intr_pin);
  1134. if (ret < 0) {
  1135. pr_err(
  1136. "[PS][CM36283 error]%s: fail to set gpio %d as input (%d)\n",
  1137. __func__, lpi->intr_pin, ret);
  1138. goto fail_free_intr_pin;
  1139. }
  1140. ret = initial_cm36283(lpi);
  1141. if (ret < 0) {
  1142. pr_err(
  1143. "[PS_ERR][CM36283 error]%s: fail to initial cm36283 (%d)\n",
  1144. __func__, ret);
  1145. goto fail_free_intr_pin;
  1146. }
  1147. /*Default disable P sensor and L sensor*/
  1148. ls_initial_cmd(lpi);
  1149. psensor_initial_cmd(lpi);
  1150. if (!lpi->polling)
  1151. ret = request_any_context_irq(lpi->irq,
  1152. cm36283_irq_handler,
  1153. IRQF_TRIGGER_LOW,
  1154. "cm36283",
  1155. lpi);
  1156. if (ret < 0) {
  1157. pr_err(
  1158. "[PS][CM36283 error]%s: req_irq(%d) fail for gpio %d (%d)\n",
  1159. __func__, lpi->irq,
  1160. lpi->intr_pin, ret);
  1161. goto fail_free_intr_pin;
  1162. }
  1163. return ret;
  1164. fail_free_intr_pin:
  1165. gpio_free(lpi->intr_pin);
  1166. return ret;
  1167. }
  1168. static int cm36283_parse_dt(struct device *dev,
  1169. struct cm36283_platform_data *pdata)
  1170. {
  1171. struct device_node *np = dev->of_node;
  1172. u32 levels[CM36283_LEVELS_SIZE], i;
  1173. u32 temp_val;
  1174. int rc;
  1175. rc = of_get_named_gpio_flags(np, "capella,interrupt-gpio",
  1176. 0, NULL);
  1177. if (rc < 0) {
  1178. dev_err(dev, "Unable to read interrupt pin number\n");
  1179. return rc;
  1180. } else {
  1181. pdata->intr = rc;
  1182. }
  1183. rc = of_property_read_u32_array(np, "capella,levels", levels,
  1184. CM36283_LEVELS_SIZE);
  1185. if (rc) {
  1186. dev_err(dev, "Unable to read levels data\n");
  1187. return rc;
  1188. } else {
  1189. for (i = 0; i < CM36283_LEVELS_SIZE; i++)
  1190. pdata->levels[i] = levels[i];
  1191. }
  1192. rc = of_property_read_u32(np, "capella,ps_close_thd_set", &temp_val);
  1193. if (rc) {
  1194. dev_err(dev, "Unable to read ps_close_thd_set\n");
  1195. return rc;
  1196. } else {
  1197. pdata->ps_close_thd_set = (u8)temp_val;
  1198. }
  1199. rc = of_property_read_u32(np, "capella,ps_away_thd_set", &temp_val);
  1200. if (rc) {
  1201. dev_err(dev, "Unable to read ps_away_thd_set\n");
  1202. return rc;
  1203. } else {
  1204. pdata->ps_away_thd_set = (u8)temp_val;
  1205. }
  1206. rc = of_property_read_u32(np, "capella,ls_cmd", &temp_val);
  1207. if (rc) {
  1208. dev_err(dev, "Unable to read ls_cmd\n");
  1209. return rc;
  1210. } else {
  1211. pdata->ls_cmd = (u16)temp_val;
  1212. }
  1213. rc = of_property_read_u32(np, "capella,ps_conf1_val", &temp_val);
  1214. if (rc) {
  1215. dev_err(dev, "Unable to read ps_conf1_val\n");
  1216. return rc;
  1217. } else {
  1218. pdata->ps_conf1_val = (u16)temp_val;
  1219. }
  1220. rc = of_property_read_u32(np, "capella,ps_conf3_val", &temp_val);
  1221. if (rc) {
  1222. dev_err(dev, "Unable to read ps_conf3_val\n");
  1223. return rc;
  1224. } else {
  1225. pdata->ps_conf3_val = (u16)temp_val;
  1226. }
  1227. pdata->polling = of_property_read_bool(np, "capella,use-polling");
  1228. return 0;
  1229. }
  1230. static int create_sysfs_interfaces(struct device *dev,
  1231. struct device_attribute *attributes, int len)
  1232. {
  1233. int i;
  1234. int err;
  1235. for (i = 0; i < len; i++) {
  1236. err = device_create_file(dev, attributes + i);
  1237. if (err)
  1238. goto error;
  1239. }
  1240. return 0;
  1241. error:
  1242. for (; i >= 0; i--)
  1243. device_remove_file(dev, attributes + i);
  1244. dev_err(dev, "%s:Unable to create interface\n", __func__);
  1245. return err;
  1246. }
  1247. static int remove_sysfs_interfaces(struct device *dev,
  1248. struct device_attribute *attributes, int len)
  1249. {
  1250. int i;
  1251. for (i = 0; i < len; i++)
  1252. device_remove_file(dev, attributes + i);
  1253. return 0;
  1254. }
  1255. static struct device_attribute light_attr[] = {
  1256. __ATTR(ls_adc, 0664, ls_adc_show, NULL),
  1257. __ATTR(ls_kadc, 0664, ls_kadc_show, ls_kadc_store),
  1258. __ATTR(ls_gadc, 0664, ls_gadc_show, ls_gadc_store),
  1259. __ATTR(ls_conf, 0664, ls_conf_show, ls_conf_store),
  1260. __ATTR(ls_adc_table, 0664,
  1261. ls_adc_table_show, ls_adc_table_store),
  1262. __ATTR(poll_delay, 0664, ls_poll_delay_show,
  1263. ls_poll_delay_store),
  1264. __ATTR(enable, 0664,
  1265. ls_enable_show, ls_enable_store),
  1266. };
  1267. static struct device_attribute proximity_attr[] = {
  1268. __ATTR(enable, 0664, ps_adc_show, ps_enable_store),
  1269. __ATTR(ps_parameters, 0664,
  1270. ps_parameters_show, ps_parameters_store),
  1271. __ATTR(ps_conf, 0664, ps_conf_show, ps_conf_store),
  1272. __ATTR(ps_hw, 0664, ps_hw_show, ps_hw_store),
  1273. __ATTR(ps_thd, 0664, ps_thd_show, ps_thd_store),
  1274. __ATTR(poll_delay, 0664, ps_poll_delay_show,
  1275. ps_poll_delay_store),
  1276. __ATTR(ls_flevel, 0664, ls_fLevel_show, ls_fLevel_store),
  1277. };
  1278. static int cm36283_probe(struct i2c_client *client,
  1279. const struct i2c_device_id *id)
  1280. {
  1281. int ret = 0;
  1282. struct cm36283_info *lpi;
  1283. struct cm36283_platform_data *pdata;
  1284. lpi = kzalloc(sizeof(struct cm36283_info), GFP_KERNEL);
  1285. if (!lpi)
  1286. return -ENOMEM;
  1287. lpi->i2c_client = client;
  1288. if (client->dev.of_node) {
  1289. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  1290. if (!pdata) {
  1291. dev_err(&client->dev, "Failed to allocate memory for pdata\n");
  1292. ret = -ENOMEM;
  1293. goto err_platform_data_null;
  1294. }
  1295. ret = cm36283_parse_dt(&client->dev, pdata);
  1296. pdata->slave_addr = client->addr;
  1297. if (ret) {
  1298. dev_err(&client->dev, "Failed to get pdata from device tree\n");
  1299. goto err_parse_dt;
  1300. }
  1301. } else {
  1302. pdata = client->dev.platform_data;
  1303. if (!pdata) {
  1304. dev_err(&client->dev, "%s: Assign platform_data error!!\n",
  1305. __func__);
  1306. ret = -EBUSY;
  1307. goto err_platform_data_null;
  1308. }
  1309. }
  1310. lpi->irq = client->irq;
  1311. i2c_set_clientdata(client, lpi);
  1312. lpi->intr_pin = pdata->intr;
  1313. lpi->adc_table = pdata->levels;
  1314. lpi->power = pdata->power;
  1315. lpi->slave_addr = pdata->slave_addr;
  1316. lpi->ps_away_thd_set = pdata->ps_away_thd_set;
  1317. lpi->ps_close_thd_set = pdata->ps_close_thd_set;
  1318. lpi->ps_conf1_val = pdata->ps_conf1_val;
  1319. lpi->ps_conf3_val = pdata->ps_conf3_val;
  1320. lpi->polling = pdata->polling;
  1321. atomic_set(&lpi->ls_poll_delay,
  1322. (unsigned int) CM36283_LS_DEFAULT_POLL_DELAY);
  1323. atomic_set(&lpi->ps_poll_delay,
  1324. (unsigned int) CM36283_PS_DEFAULT_POLL_DELAY);
  1325. lpi->ls_cmd = pdata->ls_cmd;
  1326. lpi->record_clear_int_fail=0;
  1327. dev_dbg(&lpi->i2c_client->dev, "[PS][CM36283] %s: ls_cmd 0x%x\n",
  1328. __func__, lpi->ls_cmd);
  1329. if (pdata->ls_cmd == 0) {
  1330. lpi->ls_cmd = CM36283_ALS_IT_80ms | CM36283_ALS_GAIN_2;
  1331. }
  1332. lp_info = lpi;
  1333. mutex_init(&CM36283_control_mutex);
  1334. mutex_init(&als_enable_mutex);
  1335. mutex_init(&als_disable_mutex);
  1336. mutex_init(&als_get_adc_mutex);
  1337. mutex_init(&ps_enable_mutex);
  1338. mutex_init(&ps_disable_mutex);
  1339. mutex_init(&ps_get_adc_mutex);
  1340. /*
  1341. * SET LUX STEP FACTOR HERE
  1342. * if adc raw value one step = 5/100 = 1/20 = 0.05 lux
  1343. * the following will set the factor 0.05 = 1/20
  1344. * and lpi->golden_adc = 1;
  1345. * set als_kadc = (ALS_CALIBRATED << 16) | 20;
  1346. */
  1347. als_kadc = (ALS_CALIBRATED << 16) | 10;
  1348. lpi->golden_adc = 100;
  1349. lpi->ls_calibrate = 0;
  1350. lightsensor_set_kvalue(lpi);
  1351. ret = lightsensor_update_table(lpi);
  1352. if (ret < 0) {
  1353. pr_err("[LS][CM36283 error]%s: update ls table fail\n",
  1354. __func__);
  1355. goto err_lightsensor_update_table;
  1356. }
  1357. lpi->lp_wq = create_singlethread_workqueue("cm36283_wq");
  1358. if (!lpi->lp_wq) {
  1359. pr_err("[PS][CM36283 error]%s: can't create workqueue\n", __func__);
  1360. ret = -ENOMEM;
  1361. goto err_create_singlethread_workqueue;
  1362. }
  1363. wake_lock_init(&(lpi->ps_wake_lock), WAKE_LOCK_SUSPEND, "proximity");
  1364. ret = cm36283_power_set(lpi, true);
  1365. if (ret < 0) {
  1366. dev_err(&client->dev, "%s:cm36283 power on error!\n", __func__);
  1367. goto err_cm36283_power_on;
  1368. }
  1369. ret = cm36283_setup(lpi);
  1370. if (ret < 0) {
  1371. pr_err("[PS_ERR][CM36283 error]%s: cm36283_setup error!\n", __func__);
  1372. goto err_cm36283_setup;
  1373. }
  1374. ret = lightsensor_setup(lpi);
  1375. if (ret < 0) {
  1376. pr_err("[LS][CM36283 error]%s: lightsensor_setup error!!\n",
  1377. __func__);
  1378. goto err_lightsensor_setup;
  1379. }
  1380. ret = psensor_setup(lpi);
  1381. if (ret < 0) {
  1382. pr_err("[PS][CM36283 error]%s: psensor_setup error!!\n",
  1383. __func__);
  1384. goto err_psensor_setup;
  1385. }
  1386. ret = create_sysfs_interfaces(&lpi->ls_input_dev->dev, light_attr,
  1387. ARRAY_SIZE(light_attr));
  1388. if (ret < 0) {
  1389. dev_err(&client->dev, "failed to create sysfs\n");
  1390. goto err_input_cleanup;
  1391. }
  1392. ret = create_sysfs_interfaces(&lpi->ps_input_dev->dev, proximity_attr,
  1393. ARRAY_SIZE(proximity_attr));
  1394. if (ret < 0) {
  1395. dev_err(&client->dev, "failed to create sysfs\n");
  1396. goto err_light_sysfs_cleanup;
  1397. }
  1398. lpi->als_cdev = sensors_light_cdev;
  1399. lpi->als_cdev.sensors_enable = ls_enable_set;
  1400. lpi->als_cdev.sensors_poll_delay = ls_poll_delay_set;
  1401. lpi->als_cdev.min_delay = CM36283_LS_MIN_POLL_DELAY * 1000;
  1402. lpi->ps_cdev = sensors_proximity_cdev;
  1403. lpi->ps_cdev.sensors_enable = ps_enable_set;
  1404. lpi->ps_cdev.sensors_poll_delay = ps_poll_delay_set;
  1405. lpi->ps_cdev.min_delay = CM36283_PS_MIN_POLL_DELAY * 1000;
  1406. ret = sensors_classdev_register(&client->dev, &lpi->als_cdev);
  1407. if (ret)
  1408. goto err_proximity_sysfs_cleanup;
  1409. ret = sensors_classdev_register(&client->dev, &lpi->ps_cdev);
  1410. if (ret)
  1411. goto err_create_class_sysfs;
  1412. mutex_init(&wq_lock);
  1413. INIT_DELAYED_WORK(&lpi->ldwork, lsensor_delay_work_handler);
  1414. INIT_DELAYED_WORK(&lpi->pdwork, psensor_delay_work_handler);
  1415. dev_dbg(&lpi->i2c_client->dev, "%s: Probe success!\n", __func__);
  1416. return ret;
  1417. err_create_class_sysfs:
  1418. sensors_classdev_unregister(&lpi->als_cdev);
  1419. err_proximity_sysfs_cleanup:
  1420. remove_sysfs_interfaces(&lpi->ps_input_dev->dev, proximity_attr,
  1421. ARRAY_SIZE(proximity_attr));
  1422. err_light_sysfs_cleanup:
  1423. remove_sysfs_interfaces(&lpi->ls_input_dev->dev, light_attr,
  1424. ARRAY_SIZE(light_attr));
  1425. err_input_cleanup:
  1426. input_unregister_device(lpi->ps_input_dev);
  1427. input_free_device(lpi->ps_input_dev);
  1428. err_psensor_setup:
  1429. input_unregister_device(lpi->ls_input_dev);
  1430. input_free_device(lpi->ls_input_dev);
  1431. err_lightsensor_setup:
  1432. err_cm36283_setup:
  1433. cm36283_power_set(lpi, false);
  1434. err_cm36283_power_on:
  1435. wake_lock_destroy(&(lpi->ps_wake_lock));
  1436. destroy_workqueue(lpi->lp_wq);
  1437. err_create_singlethread_workqueue:
  1438. err_lightsensor_update_table:
  1439. mutex_destroy(&CM36283_control_mutex);
  1440. mutex_destroy(&als_enable_mutex);
  1441. mutex_destroy(&als_disable_mutex);
  1442. mutex_destroy(&als_get_adc_mutex);
  1443. mutex_destroy(&ps_enable_mutex);
  1444. mutex_destroy(&ps_disable_mutex);
  1445. mutex_destroy(&ps_get_adc_mutex);
  1446. err_parse_dt:
  1447. if (client->dev.of_node && (pdata != NULL))
  1448. devm_kfree(&client->dev, pdata);
  1449. err_platform_data_null:
  1450. kfree(lpi);
  1451. dev_err(&client->dev, "%s:error exit! ret = %d\n", __func__, ret);
  1452. return ret;
  1453. }
  1454. static int control_and_report(struct cm36283_info *lpi, uint8_t mode,
  1455. uint16_t param, int report)
  1456. {
  1457. int ret = 0;
  1458. uint16_t adc_value = 0;
  1459. uint16_t ps_data = 0;
  1460. int level = 0, i, val;
  1461. mutex_lock(&CM36283_control_mutex);
  1462. if( mode == CONTROL_ALS ){
  1463. if(param){
  1464. lpi->ls_cmd &= CM36283_ALS_SD_MASK;
  1465. } else {
  1466. lpi->ls_cmd |= CM36283_ALS_SD;
  1467. }
  1468. _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd);
  1469. lpi->als_enable=param;
  1470. } else if( mode == CONTROL_PS ){
  1471. if(param){
  1472. lpi->ps_conf1_val &= CM36283_PS_SD_MASK;
  1473. lpi->ps_conf1_val |= CM36283_PS_INT_IN_AND_OUT;
  1474. } else {
  1475. lpi->ps_conf1_val |= CM36283_PS_SD;
  1476. lpi->ps_conf1_val &= CM36283_PS_INT_MASK;
  1477. }
  1478. _cm36283_I2C_Write_Word(lpi->slave_addr, PS_CONF1, lpi->ps_conf1_val);
  1479. lpi->ps_enable=param;
  1480. }
  1481. if((mode == CONTROL_ALS)||(mode == CONTROL_PS)){
  1482. if( param==1 ){
  1483. msleep(100);
  1484. }
  1485. }
  1486. if(lpi->als_enable){
  1487. if( mode == CONTROL_ALS ||
  1488. ( mode == CONTROL_INT_ISR_REPORT &&
  1489. ((param&INT_FLAG_ALS_IF_L)||(param&INT_FLAG_ALS_IF_H)))){
  1490. lpi->ls_cmd &= CM36283_ALS_INT_MASK;
  1491. ret = _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF, lpi->ls_cmd);
  1492. get_ls_adc_value(&adc_value, 0);
  1493. if( lpi->ls_calibrate ) {
  1494. for (i = 0; i < 10; i++) {
  1495. if (adc_value <= (*(lpi->cali_table + i))) {
  1496. level = i;
  1497. if (*(lpi->cali_table + i))
  1498. break;
  1499. }
  1500. if ( i == 9) {/*avoid i = 10, because 'cali_table' of size is 10 */
  1501. level = i;
  1502. break;
  1503. }
  1504. }
  1505. } else {
  1506. for (i = 0; i < 10; i++) {
  1507. if (adc_value <= (*(lpi->adc_table + i))) {
  1508. level = i;
  1509. if (*(lpi->adc_table + i))
  1510. break;
  1511. }
  1512. if ( i == 9) {/*avoid i = 10, because 'cali_table' of size is 10 */
  1513. level = i;
  1514. break;
  1515. }
  1516. }
  1517. }
  1518. if (!lpi->polling) {
  1519. ret = set_lsensor_range(((i == 0) ||
  1520. (adc_value == 0)) ? 0 :
  1521. *(lpi->cali_table + (i - 1)) + 1,
  1522. *(lpi->cali_table + i));
  1523. lpi->ls_cmd |= CM36283_ALS_INT_EN;
  1524. }
  1525. ret = _cm36283_I2C_Write_Word(lpi->slave_addr, ALS_CONF,
  1526. lpi->ls_cmd);
  1527. if (report) {
  1528. lpi->current_level = level;
  1529. lpi->current_adc = adc_value;
  1530. input_report_abs(lpi->ls_input_dev, ABS_MISC, level);
  1531. input_sync(lpi->ls_input_dev);
  1532. }
  1533. }
  1534. }
  1535. #define PS_CLOSE 1
  1536. #define PS_AWAY (1<<1)
  1537. #define PS_CLOSE_AND_AWAY PS_CLOSE+PS_AWAY
  1538. if (report && (lpi->ps_enable)) {
  1539. int ps_status = 0;
  1540. if (mode == CONTROL_PS)
  1541. ps_status = PS_CLOSE_AND_AWAY;
  1542. else if (mode == CONTROL_INT_ISR_REPORT) {
  1543. if (param & INT_FLAG_PS_IF_CLOSE)
  1544. ps_status |= PS_CLOSE;
  1545. if (param & INT_FLAG_PS_IF_AWAY)
  1546. ps_status |= PS_AWAY;
  1547. }
  1548. if (ps_status != 0) {
  1549. switch (ps_status) {
  1550. case PS_CLOSE_AND_AWAY:
  1551. get_stable_ps_adc_value(&ps_data);
  1552. val = (ps_data >= lpi->ps_close_thd_set)
  1553. ? 0 : 1;
  1554. break;
  1555. case PS_AWAY:
  1556. val = 1;
  1557. break;
  1558. case PS_CLOSE:
  1559. val = 0;
  1560. break;
  1561. };
  1562. input_report_abs(lpi->ps_input_dev, ABS_DISTANCE, val);
  1563. input_sync(lpi->ps_input_dev);
  1564. }
  1565. }
  1566. mutex_unlock(&CM36283_control_mutex);
  1567. return ret;
  1568. }
  1569. static int cm36283_power_set(struct cm36283_info *info, bool on)
  1570. {
  1571. int rc;
  1572. if (on) {
  1573. info->vdd = regulator_get(&info->i2c_client->dev, "vdd");
  1574. if (IS_ERR(info->vdd)) {
  1575. rc = PTR_ERR(info->vdd);
  1576. dev_err(&info->i2c_client->dev,
  1577. "Regulator get failed vdd rc=%d\n", rc);
  1578. goto err_vdd_get;
  1579. }
  1580. if (regulator_count_voltages(info->vdd) > 0) {
  1581. rc = regulator_set_voltage(info->vdd,
  1582. CM36283_VDD_MIN_UV, CM36283_VDD_MAX_UV);
  1583. if (rc) {
  1584. dev_err(&info->i2c_client->dev,
  1585. "Regulator set failed vdd rc=%d\n", rc);
  1586. goto err_vdd_set_vtg;
  1587. }
  1588. }
  1589. info->vio = regulator_get(&info->i2c_client->dev, "vio");
  1590. if (IS_ERR(info->vio)) {
  1591. rc = PTR_ERR(info->vio);
  1592. dev_err(&info->i2c_client->dev,
  1593. "Regulator get failed vio rc=%d\n", rc);
  1594. goto err_vio_get;
  1595. }
  1596. if (regulator_count_voltages(info->vio) > 0) {
  1597. rc = regulator_set_voltage(info->vio,
  1598. CM36283_VI2C_MIN_UV, CM36283_VI2C_MAX_UV);
  1599. if (rc) {
  1600. dev_err(&info->i2c_client->dev,
  1601. "Regulator set failed vio rc=%d\n", rc);
  1602. goto err_vio_set_vtg;
  1603. }
  1604. }
  1605. rc = regulator_enable(info->vdd);
  1606. if (rc) {
  1607. dev_err(&info->i2c_client->dev,
  1608. "Regulator vdd enable failed rc=%d\n", rc);
  1609. goto err_vdd_ena;
  1610. }
  1611. rc = regulator_enable(info->vio);
  1612. if (rc) {
  1613. dev_err(&info->i2c_client->dev,
  1614. "Regulator vio enable failed rc=%d\n", rc);
  1615. goto err_vio_ena;
  1616. }
  1617. } else {
  1618. rc = regulator_disable(info->vdd);
  1619. if (rc) {
  1620. dev_err(&info->i2c_client->dev,
  1621. "Regulator vdd disable failed rc=%d\n", rc);
  1622. return rc;
  1623. }
  1624. if (regulator_count_voltages(info->vdd) > 0)
  1625. regulator_set_voltage(info->vdd, 0, CM36283_VDD_MAX_UV);
  1626. regulator_put(info->vdd);
  1627. rc = regulator_disable(info->vio);
  1628. if (rc) {
  1629. dev_err(&info->i2c_client->dev,
  1630. "Regulator vio disable failed rc=%d\n", rc);
  1631. return rc;
  1632. }
  1633. if (regulator_count_voltages(info->vio) > 0)
  1634. regulator_set_voltage(info->vio, 0,
  1635. CM36283_VI2C_MAX_UV);
  1636. regulator_put(info->vio);
  1637. }
  1638. return 0;
  1639. err_vio_ena:
  1640. regulator_disable(info->vdd);
  1641. err_vdd_ena:
  1642. if (regulator_count_voltages(info->vio) > 0)
  1643. regulator_set_voltage(info->vio, 0, CM36283_VI2C_MAX_UV);
  1644. err_vio_set_vtg:
  1645. regulator_put(info->vio);
  1646. err_vio_get:
  1647. if (regulator_count_voltages(info->vdd) > 0)
  1648. regulator_set_voltage(info->vdd, 0, CM36283_VDD_MAX_UV);
  1649. err_vdd_set_vtg:
  1650. regulator_put(info->vdd);
  1651. err_vdd_get:
  1652. return rc;
  1653. }
  1654. #ifdef CONFIG_PM_SLEEP
  1655. static int cm36283_suspend(struct device *dev)
  1656. {
  1657. struct cm36283_info *lpi = lp_info;
  1658. if (lpi->als_enable) {
  1659. if (lightsensor_disable(lpi))
  1660. goto out;
  1661. lpi->als_enable = 1;
  1662. }
  1663. if (cm36283_power_set(lpi, 0))
  1664. goto out;
  1665. return 0;
  1666. out:
  1667. dev_err(&lpi->i2c_client->dev, "%s:failed during resume operation.\n",
  1668. __func__);
  1669. return -EIO;
  1670. }
  1671. static int cm36283_resume(struct device *dev)
  1672. {
  1673. struct cm36283_info *lpi = lp_info;
  1674. if (cm36283_power_set(lpi, 1))
  1675. goto out;
  1676. if (lpi->als_enable) {
  1677. ls_initial_cmd(lpi);
  1678. psensor_initial_cmd(lpi);
  1679. if (lightsensor_enable(lpi))
  1680. goto out;
  1681. }
  1682. return 0;
  1683. out:
  1684. dev_err(&lpi->i2c_client->dev, "%s:failed during resume operation.\n",
  1685. __func__);
  1686. return -EIO;
  1687. }
  1688. #endif
  1689. static UNIVERSAL_DEV_PM_OPS(cm36283_pm, cm36283_suspend, cm36283_resume, NULL);
  1690. static const struct i2c_device_id cm36283_i2c_id[] = {
  1691. {CM36283_I2C_NAME, 0},
  1692. {}
  1693. };
  1694. static struct of_device_id cm36283_match_table[] = {
  1695. { .compatible = "capella,cm36283",},
  1696. { },
  1697. };
  1698. static struct i2c_driver cm36283_driver = {
  1699. .id_table = cm36283_i2c_id,
  1700. .probe = cm36283_probe,
  1701. .driver = {
  1702. .name = CM36283_I2C_NAME,
  1703. .owner = THIS_MODULE,
  1704. .pm = &cm36283_pm,
  1705. .of_match_table = cm36283_match_table,
  1706. },
  1707. };
  1708. static int __init cm36283_init(void)
  1709. {
  1710. return i2c_add_driver(&cm36283_driver);
  1711. }
  1712. static void __exit cm36283_exit(void)
  1713. {
  1714. i2c_del_driver(&cm36283_driver);
  1715. }
  1716. module_init(cm36283_init);
  1717. module_exit(cm36283_exit);
  1718. MODULE_LICENSE("GPL");
  1719. MODULE_DESCRIPTION("CM36283 Driver");
  1720. MODULE_AUTHOR("Frank Hsieh <pengyueh@gmail.com>");