stk3x1x.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591
  1. /*
  2. * stk3x1x.c - Linux kernel modules for sensortek stk301x, stk321x and stk331x
  3. * proximity/ambient light sensor
  4. *
  5. * Copyright (c) 2013, The Linux Foundation. All Rights Reserved.
  6. * Copyright (C) 2012 Lex Hsieh / sensortek <lex_hsieh@sitronix.com.tw> or
  7. * <lex_hsieh@sensortek.com.tw>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * Linux Foundation chooses to take subject only to the GPLv2 license
  15. * terms, and distributes only under these terms.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/i2c.h>
  30. #include <linux/mutex.h>
  31. #include <linux/kdev_t.h>
  32. #include <linux/fs.h>
  33. #include <linux/input.h>
  34. #include <linux/sensors.h>
  35. #include <linux/workqueue.h>
  36. #include <linux/irq.h>
  37. #include <linux/delay.h>
  38. #include <linux/sched.h>
  39. #include <linux/kthread.h>
  40. #include <linux/errno.h>
  41. #include <linux/wakelock.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/gpio.h>
  44. #include <linux/fs.h>
  45. #include <linux/uaccess.h>
  46. #include <linux/regulator/consumer.h>
  47. #ifdef CONFIG_OF
  48. #include <linux/of_gpio.h>
  49. #endif
  50. #ifdef CONFIG_HAS_EARLYSUSPEND
  51. #include <linux/earlysuspend.h>
  52. #endif
  53. #include "linux/stk3x1x.h"
  54. #define DRIVER_VERSION "3.4.4ts"
  55. /* Driver Settings */
  56. #define CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
  57. #ifdef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
  58. #define STK_ALS_CHANGE_THD 20 /* The threshold to trigger ALS interrupt, unit: lux */
  59. #endif /* #ifdef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD */
  60. #define STK_INT_PS_MODE 1 /* 1, 2, or 3 */
  61. #define STK_POLL_PS
  62. #define STK_POLL_ALS /* ALS interrupt is valid only when STK_PS_INT_MODE = 1 or 4*/
  63. /* Define Register Map */
  64. #define STK_STATE_REG 0x00
  65. #define STK_PSCTRL_REG 0x01
  66. #define STK_ALSCTRL_REG 0x02
  67. #define STK_LEDCTRL_REG 0x03
  68. #define STK_INT_REG 0x04
  69. #define STK_WAIT_REG 0x05
  70. #define STK_THDH1_PS_REG 0x06
  71. #define STK_THDH2_PS_REG 0x07
  72. #define STK_THDL1_PS_REG 0x08
  73. #define STK_THDL2_PS_REG 0x09
  74. #define STK_THDH1_ALS_REG 0x0A
  75. #define STK_THDH2_ALS_REG 0x0B
  76. #define STK_THDL1_ALS_REG 0x0C
  77. #define STK_THDL2_ALS_REG 0x0D
  78. #define STK_FLAG_REG 0x10
  79. #define STK_DATA1_PS_REG 0x11
  80. #define STK_DATA2_PS_REG 0x12
  81. #define STK_DATA1_ALS_REG 0x13
  82. #define STK_DATA2_ALS_REG 0x14
  83. #define STK_DATA1_OFFSET_REG 0x15
  84. #define STK_DATA2_OFFSET_REG 0x16
  85. #define STK_DATA1_IR_REG 0x17
  86. #define STK_DATA2_IR_REG 0x18
  87. #define STK_PDT_ID_REG 0x3E
  88. #define STK_RSRVD_REG 0x3F
  89. #define STK_SW_RESET_REG 0x80
  90. /* Define state reg */
  91. #define STK_STATE_EN_IRS_SHIFT 7
  92. #define STK_STATE_EN_AK_SHIFT 6
  93. #define STK_STATE_EN_ASO_SHIFT 5
  94. #define STK_STATE_EN_IRO_SHIFT 4
  95. #define STK_STATE_EN_WAIT_SHIFT 2
  96. #define STK_STATE_EN_ALS_SHIFT 1
  97. #define STK_STATE_EN_PS_SHIFT 0
  98. #define STK_STATE_EN_IRS_MASK 0x80
  99. #define STK_STATE_EN_AK_MASK 0x40
  100. #define STK_STATE_EN_ASO_MASK 0x20
  101. #define STK_STATE_EN_IRO_MASK 0x10
  102. #define STK_STATE_EN_WAIT_MASK 0x04
  103. #define STK_STATE_EN_ALS_MASK 0x02
  104. #define STK_STATE_EN_PS_MASK 0x01
  105. /* Define PS ctrl reg */
  106. #define STK_PS_PRS_SHIFT 6
  107. #define STK_PS_GAIN_SHIFT 4
  108. #define STK_PS_IT_SHIFT 0
  109. #define STK_PS_PRS_MASK 0xC0
  110. #define STK_PS_GAIN_MASK 0x30
  111. #define STK_PS_IT_MASK 0x0F
  112. /* Define ALS ctrl reg */
  113. #define STK_ALS_PRS_SHIFT 6
  114. #define STK_ALS_GAIN_SHIFT 4
  115. #define STK_ALS_IT_SHIFT 0
  116. #define STK_ALS_PRS_MASK 0xC0
  117. #define STK_ALS_GAIN_MASK 0x30
  118. #define STK_ALS_IT_MASK 0x0F
  119. /* Define LED ctrl reg */
  120. #define STK_LED_IRDR_SHIFT 6
  121. #define STK_LED_DT_SHIFT 0
  122. #define STK_LED_IRDR_MASK 0xC0
  123. #define STK_LED_DT_MASK 0x3F
  124. /* Define interrupt reg */
  125. #define STK_INT_CTRL_SHIFT 7
  126. #define STK_INT_OUI_SHIFT 4
  127. #define STK_INT_ALS_SHIFT 3
  128. #define STK_INT_PS_SHIFT 0
  129. #define STK_INT_CTRL_MASK 0x80
  130. #define STK_INT_OUI_MASK 0x10
  131. #define STK_INT_ALS_MASK 0x08
  132. #define STK_INT_PS_MASK 0x07
  133. #define STK_INT_ALS 0x08
  134. /* Define flag reg */
  135. #define STK_FLG_ALSDR_SHIFT 7
  136. #define STK_FLG_PSDR_SHIFT 6
  137. #define STK_FLG_ALSINT_SHIFT 5
  138. #define STK_FLG_PSINT_SHIFT 4
  139. #define STK_FLG_OUI_SHIFT 2
  140. #define STK_FLG_IR_RDY_SHIFT 1
  141. #define STK_FLG_NF_SHIFT 0
  142. #define STK_FLG_ALSDR_MASK 0x80
  143. #define STK_FLG_PSDR_MASK 0x40
  144. #define STK_FLG_ALSINT_MASK 0x20
  145. #define STK_FLG_PSINT_MASK 0x10
  146. #define STK_FLG_OUI_MASK 0x04
  147. #define STK_FLG_IR_RDY_MASK 0x02
  148. #define STK_FLG_NF_MASK 0x01
  149. /* misc define */
  150. #define MIN_ALS_POLL_DELAY_NS 110000000
  151. #define DEVICE_NAME "stk_ps"
  152. #define ALS_NAME "stk3x1x-ls"
  153. #define PS_NAME "proximity"
  154. /* POWER SUPPLY VOLTAGE RANGE */
  155. #define STK3X1X_VDD_MIN_UV 2000000
  156. #define STK3X1X_VDD_MAX_UV 3300000
  157. #define STK3X1X_VIO_MIN_UV 1750000
  158. #define STK3X1X_VIO_MAX_UV 1950000
  159. #define STK_FIR_LEN 16
  160. #define MAX_FIR_LEN 32
  161. static struct sensors_classdev sensors_light_cdev = {
  162. .name = "stk3x1x-light",
  163. .vendor = "Sensortek",
  164. .version = 1,
  165. .handle = SENSORS_LIGHT_HANDLE,
  166. .type = SENSOR_TYPE_LIGHT,
  167. .max_range = "6500",
  168. .resolution = "0.0625",
  169. .sensor_power = "0.09",
  170. .min_delay = (MIN_ALS_POLL_DELAY_NS / 1000), /* us */
  171. .fifo_reserved_event_count = 0,
  172. .fifo_max_event_count = 0,
  173. .enabled = 0,
  174. .delay_msec = 200,
  175. .sensors_enable = NULL,
  176. .sensors_poll_delay = NULL,
  177. };
  178. static struct sensors_classdev sensors_proximity_cdev = {
  179. .name = "stk3x1x-proximity",
  180. .vendor = "Sensortek",
  181. .version = 1,
  182. .handle = SENSORS_PROXIMITY_HANDLE,
  183. .type = SENSOR_TYPE_PROXIMITY,
  184. .max_range = "5.0",
  185. .resolution = "5.0",
  186. .sensor_power = "0.1",
  187. .min_delay = 0,
  188. .fifo_reserved_event_count = 0,
  189. .fifo_max_event_count = 0,
  190. .enabled = 0,
  191. .delay_msec = 200,
  192. .sensors_enable = NULL,
  193. .sensors_poll_delay = NULL,
  194. };
  195. struct data_filter {
  196. u16 raw[MAX_FIR_LEN];
  197. int sum;
  198. int number;
  199. int idx;
  200. };
  201. struct stk3x1x_data {
  202. struct i2c_client *client;
  203. struct stk3x1x_platform_data *pdata;
  204. struct sensors_classdev als_cdev;
  205. struct sensors_classdev ps_cdev;
  206. #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
  207. int32_t irq;
  208. struct work_struct stk_work;
  209. struct workqueue_struct *stk_wq;
  210. #endif
  211. int int_pin;
  212. uint8_t wait_reg;
  213. #ifdef CONFIG_HAS_EARLYSUSPEND
  214. struct early_suspend stk_early_suspend;
  215. #endif
  216. uint16_t ps_thd_h;
  217. uint16_t ps_thd_l;
  218. struct mutex io_lock;
  219. struct input_dev *ps_input_dev;
  220. int32_t ps_distance_last;
  221. bool ps_enabled;
  222. struct wake_lock ps_wakelock;
  223. struct work_struct stk_ps_work;
  224. struct workqueue_struct *stk_ps_wq;
  225. #ifdef STK_POLL_PS
  226. struct wake_lock ps_nosuspend_wl;
  227. #endif
  228. struct input_dev *als_input_dev;
  229. int32_t als_lux_last;
  230. uint32_t als_transmittance;
  231. bool als_enabled;
  232. struct hrtimer als_timer;
  233. struct hrtimer ps_timer;
  234. ktime_t als_poll_delay;
  235. ktime_t ps_poll_delay;
  236. #ifdef STK_POLL_ALS
  237. struct work_struct stk_als_work;
  238. struct workqueue_struct *stk_als_wq;
  239. #endif
  240. struct regulator *vdd;
  241. struct regulator *vio;
  242. bool power_enabled;
  243. bool use_fir;
  244. struct data_filter fir;
  245. atomic_t firlength;
  246. };
  247. #if( !defined(CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD))
  248. static uint32_t lux_threshold_table[] =
  249. {
  250. 3,
  251. 10,
  252. 40,
  253. 65,
  254. 145,
  255. 300,
  256. 550,
  257. 930,
  258. 1250,
  259. 1700,
  260. };
  261. #define LUX_THD_TABLE_SIZE (sizeof(lux_threshold_table)/sizeof(uint32_t)+1)
  262. static uint16_t code_threshold_table[LUX_THD_TABLE_SIZE+1];
  263. #endif
  264. static int32_t stk3x1x_enable_ps(struct stk3x1x_data *ps_data, uint8_t enable);
  265. static int32_t stk3x1x_enable_als(struct stk3x1x_data *ps_data, uint8_t enable);
  266. static int32_t stk3x1x_set_ps_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l);
  267. static int32_t stk3x1x_set_ps_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h);
  268. static int32_t stk3x1x_set_als_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l);
  269. static int32_t stk3x1x_set_als_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h);
  270. static int stk3x1x_device_ctl(struct stk3x1x_data *ps_data, bool enable);
  271. //static int32_t stk3x1x_set_ps_aoffset(struct stk3x1x_data *ps_data, uint16_t offset);
  272. inline uint32_t stk_alscode2lux(struct stk3x1x_data *ps_data, uint32_t alscode)
  273. {
  274. alscode += ((alscode<<7)+(alscode<<3)+(alscode>>1));
  275. alscode<<=3;
  276. alscode/=ps_data->als_transmittance;
  277. return alscode;
  278. }
  279. inline uint32_t stk_lux2alscode(struct stk3x1x_data *ps_data, uint32_t lux)
  280. {
  281. lux*=ps_data->als_transmittance;
  282. lux/=1100;
  283. if (unlikely(lux>=(1<<16)))
  284. lux = (1<<16) -1;
  285. return lux;
  286. }
  287. #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
  288. static void stk_init_code_threshold_table(struct stk3x1x_data *ps_data)
  289. {
  290. uint32_t i,j;
  291. uint32_t alscode;
  292. code_threshold_table[0] = 0;
  293. #ifdef STK_DEBUG_PRINTF
  294. printk(KERN_INFO "alscode[0]=%d\n",0);
  295. #endif
  296. for (i=1,j=0;i<LUX_THD_TABLE_SIZE;i++,j++)
  297. {
  298. alscode = stk_lux2alscode(ps_data, lux_threshold_table[j]);
  299. dev_dbg(&ps_data->client->dev, "alscode[%d]=%d\n", i, alscode);
  300. code_threshold_table[i] = (uint16_t)(alscode);
  301. }
  302. code_threshold_table[i] = 0xffff;
  303. dev_dbg(&ps_data->client->dev, "alscode[%d]=%d\n", i, alscode);
  304. }
  305. static uint32_t stk_get_lux_interval_index(uint16_t alscode)
  306. {
  307. uint32_t i;
  308. for (i=1;i<=LUX_THD_TABLE_SIZE;i++)
  309. {
  310. if ((alscode>=code_threshold_table[i-1])&&(alscode<code_threshold_table[i]))
  311. {
  312. return i;
  313. }
  314. }
  315. return LUX_THD_TABLE_SIZE;
  316. }
  317. #else
  318. inline void stk_als_set_new_thd(struct stk3x1x_data *ps_data, uint16_t alscode)
  319. {
  320. int32_t high_thd,low_thd;
  321. high_thd = alscode + stk_lux2alscode(ps_data, STK_ALS_CHANGE_THD);
  322. low_thd = alscode - stk_lux2alscode(ps_data, STK_ALS_CHANGE_THD);
  323. if (high_thd >= (1<<16))
  324. high_thd = (1<<16) -1;
  325. if (low_thd <0)
  326. low_thd = 0;
  327. stk3x1x_set_als_thd_h(ps_data, (uint16_t)high_thd);
  328. stk3x1x_set_als_thd_l(ps_data, (uint16_t)low_thd);
  329. }
  330. #endif // CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
  331. static int32_t stk3x1x_init_all_reg(struct stk3x1x_data *ps_data, struct stk3x1x_platform_data *plat_data)
  332. {
  333. int32_t ret;
  334. uint8_t w_reg;
  335. w_reg = plat_data->state_reg;
  336. ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_reg);
  337. if (ret < 0)
  338. {
  339. printk(KERN_ERR "%s: write i2c error\n", __func__);
  340. return ret;
  341. }
  342. ps_data->ps_thd_h = plat_data->ps_thd_h;
  343. ps_data->ps_thd_l = plat_data->ps_thd_l;
  344. w_reg = plat_data->psctrl_reg;
  345. ret = i2c_smbus_write_byte_data(ps_data->client, STK_PSCTRL_REG, w_reg);
  346. if (ret < 0)
  347. {
  348. printk(KERN_ERR "%s: write i2c error\n", __func__);
  349. return ret;
  350. }
  351. w_reg = plat_data->alsctrl_reg;
  352. ret = i2c_smbus_write_byte_data(ps_data->client, STK_ALSCTRL_REG, w_reg);
  353. if (ret < 0)
  354. {
  355. printk(KERN_ERR "%s: write i2c error\n", __func__);
  356. return ret;
  357. }
  358. w_reg = plat_data->ledctrl_reg;
  359. ret = i2c_smbus_write_byte_data(ps_data->client, STK_LEDCTRL_REG, w_reg);
  360. if (ret < 0)
  361. {
  362. printk(KERN_ERR "%s: write i2c error\n", __func__);
  363. return ret;
  364. }
  365. ps_data->wait_reg = plat_data->wait_reg;
  366. if(ps_data->wait_reg < 2)
  367. {
  368. printk(KERN_WARNING "%s: wait_reg should be larger than 2, force to write 2\n", __func__);
  369. ps_data->wait_reg = 2;
  370. }
  371. else if (ps_data->wait_reg > 0xFF)
  372. {
  373. printk(KERN_WARNING "%s: wait_reg should be less than 0xFF, force to write 0xFF\n", __func__);
  374. ps_data->wait_reg = 0xFF;
  375. }
  376. w_reg = plat_data->wait_reg;
  377. ret = i2c_smbus_write_byte_data(ps_data->client, STK_WAIT_REG, w_reg);
  378. if (ret < 0)
  379. {
  380. printk(KERN_ERR "%s: write i2c error\n", __func__);
  381. return ret;
  382. }
  383. stk3x1x_set_ps_thd_h(ps_data, ps_data->ps_thd_h);
  384. stk3x1x_set_ps_thd_l(ps_data, ps_data->ps_thd_l);
  385. w_reg = 0;
  386. #ifndef STK_POLL_PS
  387. w_reg |= STK_INT_PS_MODE;
  388. #else
  389. w_reg |= 0x01;
  390. #endif
  391. #if (!defined(STK_POLL_ALS) && (STK_INT_PS_MODE != 0x02) && (STK_INT_PS_MODE != 0x03))
  392. w_reg |= STK_INT_ALS;
  393. #endif
  394. ret = i2c_smbus_write_byte_data(ps_data->client, STK_INT_REG, w_reg);
  395. if (ret < 0)
  396. {
  397. printk(KERN_ERR "%s: write i2c error\n", __func__);
  398. return ret;
  399. }
  400. ret = i2c_smbus_write_byte_data(ps_data->client, 0x87, 0x60);
  401. if (ret < 0) {
  402. dev_err(&ps_data->client->dev,
  403. "%s: write i2c error\n", __func__);
  404. return ret;
  405. }
  406. return 0;
  407. }
  408. static int32_t stk3x1x_check_pid(struct stk3x1x_data *ps_data)
  409. {
  410. int32_t err1, err2;
  411. err1 = i2c_smbus_read_byte_data(ps_data->client,STK_PDT_ID_REG);
  412. if (err1 < 0)
  413. {
  414. printk(KERN_ERR "%s: read i2c error, err=%d\n", __func__, err1);
  415. return err1;
  416. }
  417. err2 = i2c_smbus_read_byte_data(ps_data->client,STK_RSRVD_REG);
  418. if (err2 < 0)
  419. {
  420. printk(KERN_ERR "%s: read i2c error, err=%d\n", __func__, err2);
  421. return -1;
  422. }
  423. if(err2 == 0xC0)
  424. printk(KERN_INFO "%s: RID=0xC0!!!!!!!!!!!!!\n", __func__);
  425. return 0;
  426. }
  427. static int32_t stk3x1x_software_reset(struct stk3x1x_data *ps_data)
  428. {
  429. int32_t r;
  430. uint8_t w_reg;
  431. w_reg = 0x7F;
  432. r = i2c_smbus_write_byte_data(ps_data->client,STK_WAIT_REG,w_reg);
  433. if (r<0)
  434. {
  435. printk(KERN_ERR "%s: software reset: write i2c error, ret=%d\n", __func__, r);
  436. return r;
  437. }
  438. r = i2c_smbus_read_byte_data(ps_data->client,STK_WAIT_REG);
  439. if (w_reg != r)
  440. {
  441. printk(KERN_ERR "%s: software reset: read-back value is not the same\n", __func__);
  442. return -1;
  443. }
  444. r = i2c_smbus_write_byte_data(ps_data->client,STK_SW_RESET_REG,0);
  445. if (r<0)
  446. {
  447. printk(KERN_ERR "%s: software reset: read error after reset\n", __func__);
  448. return r;
  449. }
  450. msleep(1);
  451. return 0;
  452. }
  453. static int32_t stk3x1x_set_als_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l)
  454. {
  455. uint8_t temp;
  456. uint8_t* pSrc = (uint8_t*)&thd_l;
  457. temp = *pSrc;
  458. *pSrc = *(pSrc+1);
  459. *(pSrc+1) = temp;
  460. return i2c_smbus_write_word_data(ps_data->client,STK_THDL1_ALS_REG,thd_l);
  461. }
  462. static int32_t stk3x1x_set_als_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h)
  463. {
  464. uint8_t temp;
  465. uint8_t* pSrc = (uint8_t*)&thd_h;
  466. temp = *pSrc;
  467. *pSrc = *(pSrc+1);
  468. *(pSrc+1) = temp;
  469. return i2c_smbus_write_word_data(ps_data->client,STK_THDH1_ALS_REG,thd_h);
  470. }
  471. static int32_t stk3x1x_set_ps_thd_l(struct stk3x1x_data *ps_data, uint16_t thd_l)
  472. {
  473. uint8_t temp;
  474. uint8_t* pSrc = (uint8_t*)&thd_l;
  475. temp = *pSrc;
  476. *pSrc = *(pSrc+1);
  477. *(pSrc+1) = temp;
  478. ps_data->ps_thd_l = thd_l;
  479. return i2c_smbus_write_word_data(ps_data->client,STK_THDL1_PS_REG,thd_l);
  480. }
  481. static int32_t stk3x1x_set_ps_thd_h(struct stk3x1x_data *ps_data, uint16_t thd_h)
  482. {
  483. uint8_t temp;
  484. uint8_t* pSrc = (uint8_t*)&thd_h;
  485. temp = *pSrc;
  486. *pSrc = *(pSrc+1);
  487. *(pSrc+1) = temp;
  488. ps_data->ps_thd_h = thd_h;
  489. return i2c_smbus_write_word_data(ps_data->client,STK_THDH1_PS_REG,thd_h);
  490. }
  491. /*
  492. static int32_t stk3x1x_set_ps_foffset(struct stk3x1x_data *ps_data, uint16_t offset)
  493. {
  494. uint8_t temp;
  495. uint8_t* pSrc = (uint8_t*)&offset;
  496. temp = *pSrc;
  497. *pSrc = *(pSrc+1);
  498. *(pSrc+1) = temp;
  499. return i2c_smbus_write_word_data(ps_data->client,STK_DATA1_OFFSET_REG,offset);
  500. }
  501. static int32_t stk3x1x_set_ps_aoffset(struct stk3x1x_data *ps_data, uint16_t offset)
  502. {
  503. uint8_t temp;
  504. uint8_t* pSrc = (uint8_t*)&offset;
  505. int ret;
  506. uint8_t w_state_reg;
  507. uint8_t re_en;
  508. ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
  509. if (ret < 0)
  510. {
  511. printk(KERN_ERR "%s: write i2c error\n", __func__);
  512. return ret;
  513. }
  514. re_en = (ret & STK_STATE_EN_AK_MASK) ? 1: 0;
  515. if(re_en)
  516. {
  517. w_state_reg = (uint8_t)(ret & (~STK_STATE_EN_AK_MASK));
  518. ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
  519. if (ret < 0)
  520. {
  521. printk(KERN_ERR "%s: write i2c error\n", __func__);
  522. return ret;
  523. }
  524. msleep(1);
  525. }
  526. temp = *pSrc;
  527. *pSrc = *(pSrc+1);
  528. *(pSrc+1) = temp;
  529. ret = i2c_smbus_write_word_data(ps_data->client,0x0E,offset);
  530. if(!re_en)
  531. return ret;
  532. w_state_reg |= STK_STATE_EN_AK_MASK;
  533. ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
  534. if (ret < 0)
  535. {
  536. printk(KERN_ERR "%s: write i2c error\n", __func__);
  537. return ret;
  538. }
  539. return 0;
  540. }
  541. */
  542. static inline uint32_t stk3x1x_get_ps_reading(struct stk3x1x_data *ps_data)
  543. {
  544. int32_t word_data, tmp_word_data;
  545. tmp_word_data = i2c_smbus_read_word_data(ps_data->client,STK_DATA1_PS_REG);
  546. if(tmp_word_data < 0)
  547. {
  548. printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data);
  549. return tmp_word_data;
  550. }
  551. word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ;
  552. return word_data;
  553. }
  554. static int32_t stk3x1x_set_flag(struct stk3x1x_data *ps_data, uint8_t org_flag_reg, uint8_t clr)
  555. {
  556. uint8_t w_flag;
  557. w_flag = org_flag_reg | (STK_FLG_ALSINT_MASK | STK_FLG_PSINT_MASK | STK_FLG_OUI_MASK | STK_FLG_IR_RDY_MASK);
  558. w_flag &= (~clr);
  559. //printk(KERN_INFO "%s: org_flag_reg=0x%x, w_flag = 0x%x\n", __func__, org_flag_reg, w_flag);
  560. return i2c_smbus_write_byte_data(ps_data->client,STK_FLAG_REG, w_flag);
  561. }
  562. static int32_t stk3x1x_get_flag(struct stk3x1x_data *ps_data)
  563. {
  564. return i2c_smbus_read_byte_data(ps_data->client,STK_FLAG_REG);
  565. }
  566. static int32_t stk3x1x_enable_ps(struct stk3x1x_data *ps_data, uint8_t enable)
  567. {
  568. int32_t ret;
  569. uint8_t w_state_reg;
  570. uint8_t curr_ps_enable;
  571. curr_ps_enable = ps_data->ps_enabled?1:0;
  572. if(curr_ps_enable == enable)
  573. return 0;
  574. if (enable) {
  575. ret = stk3x1x_device_ctl(ps_data, enable);
  576. if (ret)
  577. return ret;
  578. }
  579. ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
  580. if (ret < 0)
  581. {
  582. printk(KERN_ERR "%s: write i2c error, ret=%d\n", __func__, ret);
  583. return ret;
  584. }
  585. w_state_reg = ret;
  586. w_state_reg &= ~(STK_STATE_EN_PS_MASK | STK_STATE_EN_WAIT_MASK | 0x60);
  587. if(enable)
  588. {
  589. w_state_reg |= STK_STATE_EN_PS_MASK;
  590. if(!(ps_data->als_enabled))
  591. w_state_reg |= STK_STATE_EN_WAIT_MASK;
  592. }
  593. ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
  594. if (ret < 0)
  595. {
  596. printk(KERN_ERR "%s: write i2c error, ret=%d\n", __func__, ret);
  597. return ret;
  598. }
  599. if(enable)
  600. {
  601. #ifdef STK_POLL_PS
  602. hrtimer_start(&ps_data->ps_timer, ps_data->ps_poll_delay, HRTIMER_MODE_REL);
  603. ps_data->ps_distance_last = -1;
  604. #endif
  605. ps_data->ps_enabled = true;
  606. #ifndef STK_POLL_PS
  607. #ifndef STK_POLL_ALS
  608. if(!(ps_data->als_enabled))
  609. #endif /* #ifndef STK_POLL_ALS */
  610. enable_irq(ps_data->irq);
  611. msleep(1);
  612. ret = stk3x1x_get_flag(ps_data);
  613. if (ret < 0)
  614. {
  615. printk(KERN_ERR "%s: read i2c error, ret=%d\n", __func__, ret);
  616. return ret;
  617. }
  618. near_far_state = ret & STK_FLG_NF_MASK;
  619. ps_data->ps_distance_last = near_far_state;
  620. input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
  621. input_sync(ps_data->ps_input_dev);
  622. wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
  623. reading = stk3x1x_get_ps_reading(ps_data);
  624. dev_dbg(&ps_data->client->dev,
  625. "%s: ps input event=%d, ps code = %d\n",
  626. __func__, near_far_state, reading);
  627. #endif /* #ifndef STK_POLL_PS */
  628. }
  629. else
  630. {
  631. #ifdef STK_POLL_PS
  632. hrtimer_cancel(&ps_data->ps_timer);
  633. #else
  634. #ifndef STK_POLL_ALS
  635. if(!(ps_data->als_enabled))
  636. #endif
  637. disable_irq(ps_data->irq);
  638. #endif
  639. ps_data->ps_enabled = false;
  640. }
  641. if (!enable) {
  642. ret = stk3x1x_device_ctl(ps_data, enable);
  643. if (ret)
  644. return ret;
  645. }
  646. return ret;
  647. }
  648. static int32_t stk3x1x_enable_als(struct stk3x1x_data *ps_data, uint8_t enable)
  649. {
  650. int32_t ret;
  651. uint8_t w_state_reg;
  652. uint8_t curr_als_enable = (ps_data->als_enabled)?1:0;
  653. if(curr_als_enable == enable)
  654. return 0;
  655. if (enable) {
  656. ret = stk3x1x_device_ctl(ps_data, enable);
  657. if (ret)
  658. return ret;
  659. }
  660. #ifndef STK_POLL_ALS
  661. if (enable)
  662. {
  663. stk3x1x_set_als_thd_h(ps_data, 0x0000);
  664. stk3x1x_set_als_thd_l(ps_data, 0xFFFF);
  665. }
  666. #endif
  667. ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
  668. if (ret < 0)
  669. {
  670. printk(KERN_ERR "%s: write i2c error\n", __func__);
  671. return ret;
  672. }
  673. w_state_reg = (uint8_t)(ret & (~(STK_STATE_EN_ALS_MASK | STK_STATE_EN_WAIT_MASK)));
  674. if(enable)
  675. w_state_reg |= STK_STATE_EN_ALS_MASK;
  676. else if (ps_data->ps_enabled)
  677. w_state_reg |= STK_STATE_EN_WAIT_MASK;
  678. ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
  679. if (ret < 0)
  680. {
  681. printk(KERN_ERR "%s: write i2c error\n", __func__);
  682. return ret;
  683. }
  684. if (enable)
  685. {
  686. ps_data->als_enabled = true;
  687. #ifdef STK_POLL_ALS
  688. hrtimer_start(&ps_data->als_timer, ps_data->als_poll_delay, HRTIMER_MODE_REL);
  689. #else
  690. #ifndef STK_POLL_PS
  691. if(!(ps_data->ps_enabled))
  692. #endif
  693. enable_irq(ps_data->irq);
  694. #endif
  695. }
  696. else
  697. {
  698. ps_data->als_enabled = false;
  699. #ifdef STK_POLL_ALS
  700. hrtimer_cancel(&ps_data->als_timer);
  701. #else
  702. #ifndef STK_POLL_PS
  703. if(!(ps_data->ps_enabled))
  704. #endif
  705. disable_irq(ps_data->irq);
  706. #endif
  707. }
  708. if (!enable) {
  709. ret = stk3x1x_device_ctl(ps_data, enable);
  710. if (ret)
  711. return ret;
  712. }
  713. return ret;
  714. }
  715. static inline int32_t stk3x1x_filter_reading(struct stk3x1x_data *ps_data,
  716. int32_t word_data)
  717. {
  718. int index;
  719. int firlen = atomic_read(&ps_data->firlength);
  720. if (ps_data->fir.number < firlen) {
  721. ps_data->fir.raw[ps_data->fir.number] = word_data;
  722. ps_data->fir.sum += word_data;
  723. ps_data->fir.number++;
  724. ps_data->fir.idx++;
  725. } else {
  726. index = ps_data->fir.idx % firlen;
  727. ps_data->fir.sum -= ps_data->fir.raw[index];
  728. ps_data->fir.raw[index] = word_data;
  729. ps_data->fir.sum += word_data;
  730. ps_data->fir.idx++;
  731. word_data = ps_data->fir.sum/firlen;
  732. }
  733. return word_data;
  734. }
  735. static inline int32_t stk3x1x_get_als_reading(struct stk3x1x_data *ps_data)
  736. {
  737. int32_t word_data, tmp_word_data;
  738. tmp_word_data = i2c_smbus_read_word_data(ps_data->client, STK_DATA1_ALS_REG);
  739. if(tmp_word_data < 0)
  740. {
  741. printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data);
  742. return tmp_word_data;
  743. }
  744. word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ;
  745. if (ps_data->use_fir)
  746. word_data = stk3x1x_filter_reading(ps_data, word_data);
  747. return word_data;
  748. }
  749. static int32_t stk3x1x_get_ir_reading(struct stk3x1x_data *ps_data)
  750. {
  751. int32_t word_data, tmp_word_data;
  752. int32_t ret;
  753. uint8_t w_reg, retry = 0;
  754. if(ps_data->ps_enabled)
  755. {
  756. stk3x1x_enable_ps(ps_data, 0);
  757. ps_data->ps_enabled = true;
  758. }
  759. ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
  760. if (ret < 0)
  761. {
  762. printk(KERN_ERR "%s: write i2c error\n", __func__);
  763. return ret;
  764. }
  765. w_reg = (uint8_t)(ret & (~STK_STATE_EN_IRS_MASK));
  766. w_reg |= STK_STATE_EN_IRS_MASK;
  767. ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_reg);
  768. if (ret < 0)
  769. {
  770. printk(KERN_ERR "%s: write i2c error\n", __func__);
  771. return ret;
  772. }
  773. msleep(100);
  774. do
  775. {
  776. msleep(50);
  777. ret = stk3x1x_get_flag(ps_data);
  778. if (ret < 0)
  779. {
  780. printk(KERN_ERR "%s: write i2c error\n", __func__);
  781. return ret;
  782. }
  783. retry++;
  784. }while(retry < 5 && ((ret&STK_FLG_IR_RDY_MASK) == 0));
  785. if(retry == 5)
  786. {
  787. printk(KERN_ERR "%s: ir data is not ready for 300ms\n", __func__);
  788. return -EINVAL;
  789. }
  790. ret = stk3x1x_get_flag(ps_data);
  791. if (ret < 0)
  792. {
  793. printk(KERN_ERR "%s: write i2c error\n", __func__);
  794. return ret;
  795. }
  796. ret = stk3x1x_set_flag(ps_data, ret, STK_FLG_IR_RDY_MASK);
  797. if (ret < 0)
  798. {
  799. printk(KERN_ERR "%s: write i2c error\n", __func__);
  800. return ret;
  801. }
  802. tmp_word_data = i2c_smbus_read_word_data(ps_data->client, STK_DATA1_IR_REG);
  803. if(tmp_word_data < 0)
  804. {
  805. printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data);
  806. return tmp_word_data;
  807. }
  808. word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ;
  809. if(ps_data->ps_enabled)
  810. stk3x1x_enable_ps(ps_data, 1);
  811. return word_data;
  812. }
  813. static ssize_t stk_als_code_show(struct device *dev, struct device_attribute *attr, char *buf)
  814. {
  815. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  816. int32_t reading;
  817. reading = stk3x1x_get_als_reading(ps_data);
  818. return scnprintf(buf, PAGE_SIZE, "%d\n", reading);
  819. }
  820. static ssize_t stk_als_enable_set(struct sensors_classdev *sensors_cdev,
  821. unsigned int enabled)
  822. {
  823. struct stk3x1x_data *als_data = container_of(sensors_cdev,
  824. struct stk3x1x_data, als_cdev);
  825. int err;
  826. mutex_lock(&als_data->io_lock);
  827. err = stk3x1x_enable_als(als_data, enabled);
  828. mutex_unlock(&als_data->io_lock);
  829. if (err < 0)
  830. return err;
  831. return 0;
  832. }
  833. static ssize_t stk_als_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  834. {
  835. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  836. int32_t enable, ret;
  837. mutex_lock(&ps_data->io_lock);
  838. enable = (ps_data->als_enabled)?1:0;
  839. mutex_unlock(&ps_data->io_lock);
  840. ret = i2c_smbus_read_byte_data(ps_data->client,STK_STATE_REG);
  841. ret = (ret & STK_STATE_EN_ALS_MASK)?1:0;
  842. if(enable != ret)
  843. printk(KERN_ERR "%s: driver and sensor mismatch! driver_enable=0x%x, sensor_enable=%x\n", __func__, enable, ret);
  844. return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
  845. }
  846. static ssize_t stk_als_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  847. {
  848. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  849. uint8_t en;
  850. if (sysfs_streq(buf, "1"))
  851. en = 1;
  852. else if (sysfs_streq(buf, "0"))
  853. en = 0;
  854. else
  855. {
  856. printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf);
  857. return -EINVAL;
  858. }
  859. dev_dbg(dev, "%s: Enable ALS : %d\n", __func__, en);
  860. mutex_lock(&ps_data->io_lock);
  861. stk3x1x_enable_als(ps_data, en);
  862. mutex_unlock(&ps_data->io_lock);
  863. return size;
  864. }
  865. static ssize_t stk_als_lux_show(struct device *dev, struct device_attribute *attr, char *buf)
  866. {
  867. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  868. int32_t als_reading;
  869. uint32_t als_lux;
  870. als_reading = stk3x1x_get_als_reading(ps_data);
  871. mutex_lock(&ps_data->io_lock);
  872. als_lux = stk_alscode2lux(ps_data, als_reading);
  873. mutex_unlock(&ps_data->io_lock);
  874. return scnprintf(buf, PAGE_SIZE, "%d lux\n", als_lux);
  875. }
  876. static ssize_t stk_als_lux_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  877. {
  878. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  879. unsigned long value = 0;
  880. int ret;
  881. ret = kstrtoul(buf, 16, &value);
  882. if(ret < 0)
  883. {
  884. printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
  885. __func__, ret);
  886. return ret;
  887. }
  888. mutex_lock(&ps_data->io_lock);
  889. ps_data->als_lux_last = value;
  890. input_report_abs(ps_data->als_input_dev, ABS_MISC, value);
  891. input_sync(ps_data->als_input_dev);
  892. mutex_unlock(&ps_data->io_lock);
  893. dev_dbg(dev, "%s: als input event %ld lux\n", __func__, value);
  894. return size;
  895. }
  896. static ssize_t stk_als_transmittance_show(struct device *dev, struct device_attribute *attr, char *buf)
  897. {
  898. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  899. int32_t transmittance;
  900. mutex_lock(&ps_data->io_lock);
  901. transmittance = ps_data->als_transmittance;
  902. mutex_unlock(&ps_data->io_lock);
  903. return scnprintf(buf, PAGE_SIZE, "%d\n", transmittance);
  904. }
  905. static ssize_t stk_als_transmittance_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  906. {
  907. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  908. unsigned long value = 0;
  909. int ret;
  910. ret = kstrtoul(buf, 10, &value);
  911. if(ret < 0)
  912. {
  913. printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
  914. __func__, ret);
  915. return ret;
  916. }
  917. mutex_lock(&ps_data->io_lock);
  918. ps_data->als_transmittance = value;
  919. mutex_unlock(&ps_data->io_lock);
  920. return size;
  921. }
  922. static ssize_t stk_als_delay_show(struct device *dev, struct device_attribute *attr, char *buf)
  923. {
  924. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  925. return scnprintf(buf, PAGE_SIZE, "%u\n",
  926. (u32)ktime_to_ms(ps_data->als_poll_delay));
  927. }
  928. static inline void stk_als_delay_store_fir(struct stk3x1x_data *ps_data)
  929. {
  930. ps_data->fir.number = 0;
  931. ps_data->fir.idx = 0;
  932. ps_data->fir.sum = 0;
  933. }
  934. static ssize_t stk_als_poll_delay_set(struct sensors_classdev *sensors_cdev,
  935. unsigned int delay_msec)
  936. {
  937. struct stk3x1x_data *als_data = container_of(sensors_cdev,
  938. struct stk3x1x_data, als_cdev);
  939. uint64_t value = 0;
  940. value = delay_msec * 1000000;
  941. if (value < MIN_ALS_POLL_DELAY_NS)
  942. value = MIN_ALS_POLL_DELAY_NS;
  943. mutex_lock(&als_data->io_lock);
  944. if (value != ktime_to_ns(als_data->als_poll_delay))
  945. als_data->als_poll_delay = ns_to_ktime(value);
  946. if (als_data->use_fir)
  947. stk_als_delay_store_fir(als_data);
  948. mutex_unlock(&als_data->io_lock);
  949. return 0;
  950. }
  951. static ssize_t stk_als_delay_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  952. {
  953. uint64_t value = 0;
  954. int ret;
  955. struct stk3x1x_data *als_data = dev_get_drvdata(dev);
  956. ret = kstrtoull(buf, 10, &value);
  957. if(ret < 0)
  958. {
  959. dev_err(dev, "%s:kstrtoull failed, ret=0x%x\n", __func__, ret);
  960. return ret;
  961. }
  962. #ifdef STK_DEBUG_PRINTF
  963. dev_dbg(dev, "%s: set als poll delay=%lld\n", __func__, value);
  964. #endif
  965. ret = stk_als_poll_delay_set(&als_data->als_cdev, value);
  966. if (ret < 0)
  967. return ret;
  968. return size;
  969. }
  970. static ssize_t stk_als_ir_code_show(struct device *dev, struct device_attribute *attr, char *buf)
  971. {
  972. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  973. int32_t reading;
  974. reading = stk3x1x_get_ir_reading(ps_data);
  975. return scnprintf(buf, PAGE_SIZE, "%d\n", reading);
  976. }
  977. static ssize_t stk_als_firlen_show(struct device *dev,
  978. struct device_attribute *attr, char *buf)
  979. {
  980. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  981. int len = atomic_read(&ps_data->firlength);
  982. dev_dbg(dev, "%s: len = %2d, idx = %2d\n",
  983. __func__, len, ps_data->fir.idx);
  984. dev_dbg(dev, "%s: sum = %5d, ave = %5d\n",
  985. __func__, ps_data->fir.sum, ps_data->fir.sum/len);
  986. return scnprintf(buf, PAGE_SIZE, "%d\n", len);
  987. }
  988. static ssize_t stk_als_firlen_store(struct device *dev,
  989. struct device_attribute *attr,
  990. const char *buf, size_t size)
  991. {
  992. uint64_t value = 0;
  993. int ret;
  994. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  995. ret = kstrtoull(buf, 10, &value);
  996. if (ret < 0) {
  997. dev_err(dev, "%s:strict_strtoull failed, ret=0x%x\n",
  998. __func__, ret);
  999. return ret;
  1000. }
  1001. if (value > MAX_FIR_LEN) {
  1002. dev_err(dev, "%s: firlen exceed maximum filter length\n",
  1003. __func__);
  1004. } else if (value < 1) {
  1005. atomic_set(&ps_data->firlength, 1);
  1006. memset(&ps_data->fir, 0x00, sizeof(ps_data->fir));
  1007. } else {
  1008. atomic_set(&ps_data->firlength, value);
  1009. memset(&ps_data->fir, 0x00, sizeof(ps_data->fir));
  1010. }
  1011. return size;
  1012. }
  1013. static ssize_t stk_als_fir_enable_show(struct device *dev,
  1014. struct device_attribute *attr, char *buf)
  1015. {
  1016. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1017. return scnprintf(buf, PAGE_SIZE, "%d\n", ps_data->use_fir);
  1018. }
  1019. static ssize_t stk_als_fir_enable_store(struct device *dev,
  1020. struct device_attribute *attr,
  1021. const char *buf, size_t size)
  1022. {
  1023. uint64_t value = 0;
  1024. int ret;
  1025. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1026. ret = kstrtoull(buf, 10, &value);
  1027. if (ret < 0) {
  1028. dev_err(dev, "%s:strict_strtoull failed, ret=0x%x\n",
  1029. __func__, ret);
  1030. return ret;
  1031. }
  1032. if (value) {
  1033. ps_data->use_fir = true;
  1034. memset(&ps_data->fir, 0x00, sizeof(ps_data->fir));
  1035. } else {
  1036. ps_data->use_fir = false;
  1037. }
  1038. return size;
  1039. }
  1040. static ssize_t stk_ps_code_show(struct device *dev, struct device_attribute *attr, char *buf)
  1041. {
  1042. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1043. uint32_t reading;
  1044. reading = stk3x1x_get_ps_reading(ps_data);
  1045. return scnprintf(buf, PAGE_SIZE, "%d\n", reading);
  1046. }
  1047. static ssize_t stk_ps_enable_set(struct sensors_classdev *sensors_cdev,
  1048. unsigned int enabled)
  1049. {
  1050. struct stk3x1x_data *ps_data = container_of(sensors_cdev,
  1051. struct stk3x1x_data, ps_cdev);
  1052. int err;
  1053. mutex_lock(&ps_data->io_lock);
  1054. err = stk3x1x_enable_ps(ps_data, enabled);
  1055. mutex_unlock(&ps_data->io_lock);
  1056. if (err < 0)
  1057. return err;
  1058. return 0;
  1059. }
  1060. static ssize_t stk_ps_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  1061. {
  1062. int32_t enable, ret;
  1063. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1064. mutex_lock(&ps_data->io_lock);
  1065. enable = (ps_data->ps_enabled)?1:0;
  1066. mutex_unlock(&ps_data->io_lock);
  1067. ret = i2c_smbus_read_byte_data(ps_data->client,STK_STATE_REG);
  1068. ret = (ret & STK_STATE_EN_PS_MASK)?1:0;
  1069. if(enable != ret)
  1070. printk(KERN_ERR "%s: driver and sensor mismatch! driver_enable=0x%x, sensor_enable=%x\n", __func__, enable, ret);
  1071. return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
  1072. }
  1073. static ssize_t stk_ps_enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  1074. {
  1075. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1076. uint8_t en;
  1077. if (sysfs_streq(buf, "1"))
  1078. en = 1;
  1079. else if (sysfs_streq(buf, "0"))
  1080. en = 0;
  1081. else
  1082. {
  1083. printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf);
  1084. return -EINVAL;
  1085. }
  1086. dev_dbg(dev, "%s: Enable PS : %d\n", __func__, en);
  1087. mutex_lock(&ps_data->io_lock);
  1088. stk3x1x_enable_ps(ps_data, en);
  1089. mutex_unlock(&ps_data->io_lock);
  1090. return size;
  1091. }
  1092. static ssize_t stk_ps_enable_aso_show(struct device *dev, struct device_attribute *attr, char *buf)
  1093. {
  1094. int32_t ret;
  1095. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1096. ret = i2c_smbus_read_byte_data(ps_data->client,STK_STATE_REG);
  1097. ret = (ret & STK_STATE_EN_ASO_MASK)?1:0;
  1098. return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
  1099. }
  1100. static ssize_t stk_ps_enable_aso_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  1101. {
  1102. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1103. uint8_t en;
  1104. int32_t ret;
  1105. uint8_t w_state_reg;
  1106. if (sysfs_streq(buf, "1"))
  1107. en = 1;
  1108. else if (sysfs_streq(buf, "0"))
  1109. en = 0;
  1110. else
  1111. {
  1112. printk(KERN_ERR "%s, invalid value %d\n", __func__, *buf);
  1113. return -EINVAL;
  1114. }
  1115. dev_dbg(dev, "%s: Enable PS ASO : %d\n", __func__, en);
  1116. ret = i2c_smbus_read_byte_data(ps_data->client, STK_STATE_REG);
  1117. if (ret < 0)
  1118. {
  1119. printk(KERN_ERR "%s: write i2c error\n", __func__);
  1120. return ret;
  1121. }
  1122. w_state_reg = (uint8_t)(ret & (~STK_STATE_EN_ASO_MASK));
  1123. if(en)
  1124. w_state_reg |= STK_STATE_EN_ASO_MASK;
  1125. ret = i2c_smbus_write_byte_data(ps_data->client, STK_STATE_REG, w_state_reg);
  1126. if (ret < 0)
  1127. {
  1128. printk(KERN_ERR "%s: write i2c error\n", __func__);
  1129. return ret;
  1130. }
  1131. return size;
  1132. }
  1133. static ssize_t stk_ps_offset_show(struct device *dev, struct device_attribute *attr, char *buf)
  1134. {
  1135. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1136. int32_t word_data, tmp_word_data;
  1137. tmp_word_data = i2c_smbus_read_word_data(ps_data->client, STK_DATA1_OFFSET_REG);
  1138. if(tmp_word_data < 0)
  1139. {
  1140. printk(KERN_ERR "%s fail, err=0x%x", __func__, tmp_word_data);
  1141. return tmp_word_data;
  1142. }
  1143. word_data = ((tmp_word_data & 0xFF00) >> 8) | ((tmp_word_data & 0x00FF) << 8) ;
  1144. return scnprintf(buf, PAGE_SIZE, "%d\n", word_data);
  1145. }
  1146. static ssize_t stk_ps_offset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  1147. {
  1148. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1149. unsigned long value = 0;
  1150. int ret;
  1151. uint16_t offset;
  1152. ret = kstrtoul(buf, 10, &value);
  1153. if(ret < 0)
  1154. {
  1155. printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
  1156. __func__, ret);
  1157. return ret;
  1158. }
  1159. if(value > 65535)
  1160. {
  1161. printk(KERN_ERR "%s: invalid value, offset=%ld\n", __func__, value);
  1162. return -EINVAL;
  1163. }
  1164. offset = (uint16_t) ((value&0x00FF) << 8) | ((value&0xFF00) >>8);
  1165. ret = i2c_smbus_write_word_data(ps_data->client,STK_DATA1_OFFSET_REG,offset);
  1166. if(ret < 0)
  1167. {
  1168. printk(KERN_ERR "%s: write i2c error\n", __func__);
  1169. return ret;
  1170. }
  1171. return size;
  1172. }
  1173. static ssize_t stk_ps_distance_show(struct device *dev, struct device_attribute *attr, char *buf)
  1174. {
  1175. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1176. int32_t dist=1, ret;
  1177. mutex_lock(&ps_data->io_lock);
  1178. ret = stk3x1x_get_flag(ps_data);
  1179. if(ret < 0)
  1180. {
  1181. printk(KERN_ERR "%s: stk3x1x_get_flag failed, ret=0x%x\n", __func__, ret);
  1182. return ret;
  1183. }
  1184. dist = (ret & STK_FLG_NF_MASK)?1:0;
  1185. ps_data->ps_distance_last = dist;
  1186. input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, dist);
  1187. input_sync(ps_data->ps_input_dev);
  1188. mutex_unlock(&ps_data->io_lock);
  1189. wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
  1190. dev_dbg(dev, "%s: ps input event %d cm\n", __func__, dist);
  1191. return scnprintf(buf, PAGE_SIZE, "%d\n", dist);
  1192. }
  1193. static ssize_t stk_ps_distance_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  1194. {
  1195. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1196. unsigned long value = 0;
  1197. int ret;
  1198. ret = kstrtoul(buf, 10, &value);
  1199. if(ret < 0)
  1200. {
  1201. printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
  1202. __func__, ret);
  1203. return ret;
  1204. }
  1205. mutex_lock(&ps_data->io_lock);
  1206. ps_data->ps_distance_last = value;
  1207. input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, value);
  1208. input_sync(ps_data->ps_input_dev);
  1209. mutex_unlock(&ps_data->io_lock);
  1210. wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
  1211. dev_dbg(dev, "%s: ps input event %ld cm\n", __func__, value);
  1212. return size;
  1213. }
  1214. static ssize_t stk_ps_code_thd_l_show(struct device *dev, struct device_attribute *attr, char *buf)
  1215. {
  1216. int32_t ps_thd_l1_reg, ps_thd_l2_reg;
  1217. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1218. mutex_lock(&ps_data->io_lock);
  1219. ps_thd_l1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL1_PS_REG);
  1220. if(ps_thd_l1_reg < 0)
  1221. {
  1222. printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_l1_reg);
  1223. return -EINVAL;
  1224. }
  1225. ps_thd_l2_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL2_PS_REG);
  1226. if(ps_thd_l2_reg < 0)
  1227. {
  1228. printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_l2_reg);
  1229. return -EINVAL;
  1230. }
  1231. mutex_unlock(&ps_data->io_lock);
  1232. ps_thd_l1_reg = ps_thd_l1_reg<<8 | ps_thd_l2_reg;
  1233. return scnprintf(buf, PAGE_SIZE, "%d\n", ps_thd_l1_reg);
  1234. }
  1235. static ssize_t stk_ps_code_thd_l_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  1236. {
  1237. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1238. unsigned long value = 0;
  1239. int ret;
  1240. ret = kstrtoul(buf, 10, &value);
  1241. if(ret < 0)
  1242. {
  1243. printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
  1244. __func__, ret);
  1245. return ret;
  1246. }
  1247. mutex_lock(&ps_data->io_lock);
  1248. stk3x1x_set_ps_thd_l(ps_data, value);
  1249. mutex_unlock(&ps_data->io_lock);
  1250. return size;
  1251. }
  1252. static ssize_t stk_ps_code_thd_h_show(struct device *dev, struct device_attribute *attr, char *buf)
  1253. {
  1254. int32_t ps_thd_h1_reg, ps_thd_h2_reg;
  1255. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1256. mutex_lock(&ps_data->io_lock);
  1257. ps_thd_h1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH1_PS_REG);
  1258. if(ps_thd_h1_reg < 0)
  1259. {
  1260. printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_h1_reg);
  1261. return -EINVAL;
  1262. }
  1263. ps_thd_h2_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH2_PS_REG);
  1264. if(ps_thd_h2_reg < 0)
  1265. {
  1266. printk(KERN_ERR "%s fail, err=0x%x", __func__, ps_thd_h2_reg);
  1267. return -EINVAL;
  1268. }
  1269. mutex_unlock(&ps_data->io_lock);
  1270. ps_thd_h1_reg = ps_thd_h1_reg<<8 | ps_thd_h2_reg;
  1271. return scnprintf(buf, PAGE_SIZE, "%d\n", ps_thd_h1_reg);
  1272. }
  1273. static ssize_t stk_ps_code_thd_h_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  1274. {
  1275. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1276. unsigned long value = 0;
  1277. int ret;
  1278. ret = kstrtoul(buf, 10, &value);
  1279. if(ret < 0)
  1280. {
  1281. printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
  1282. __func__, ret);
  1283. return ret;
  1284. }
  1285. mutex_lock(&ps_data->io_lock);
  1286. stk3x1x_set_ps_thd_h(ps_data, value);
  1287. mutex_unlock(&ps_data->io_lock);
  1288. return size;
  1289. }
  1290. #if 0
  1291. static ssize_t stk_als_lux_thd_l_show(struct device *dev, struct device_attribute *attr, char *buf)
  1292. {
  1293. int32_t als_thd_l0_reg,als_thd_l1_reg;
  1294. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1295. uint32_t als_lux;
  1296. mutex_lock(&ps_data->io_lock);
  1297. als_thd_l0_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL1_ALS_REG);
  1298. als_thd_l1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDL2_ALS_REG);
  1299. if(als_thd_l0_reg < 0)
  1300. {
  1301. printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_l0_reg);
  1302. return -EINVAL;
  1303. }
  1304. if(als_thd_l1_reg < 0)
  1305. {
  1306. printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_l1_reg);
  1307. return -EINVAL;
  1308. }
  1309. als_thd_l0_reg|=(als_thd_l1_reg<<8);
  1310. als_lux = stk_alscode2lux(ps_data, als_thd_l0_reg);
  1311. mutex_unlock(&ps_data->io_lock);
  1312. return scnprintf(buf, PAGE_SIZE, "%d\n", als_lux);
  1313. }
  1314. static ssize_t stk_als_lux_thd_l_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  1315. {
  1316. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1317. unsigned long value = 0;
  1318. int ret;
  1319. ret = kstrtoul(buf, 10, &value);
  1320. if(ret < 0)
  1321. {
  1322. printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
  1323. __func__, ret);
  1324. return ret;
  1325. }
  1326. mutex_lock(&ps_data->io_lock);
  1327. value = stk_lux2alscode(ps_data, value);
  1328. stk3x1x_set_als_thd_l(ps_data, value);
  1329. mutex_unlock(&ps_data->io_lock);
  1330. return size;
  1331. }
  1332. static ssize_t stk_als_lux_thd_h_show(struct device *dev, struct device_attribute *attr, char *buf)
  1333. {
  1334. int32_t als_thd_h0_reg,als_thd_h1_reg;
  1335. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1336. uint32_t als_lux;
  1337. mutex_lock(&ps_data->io_lock);
  1338. als_thd_h0_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH1_ALS_REG);
  1339. als_thd_h1_reg = i2c_smbus_read_byte_data(ps_data->client,STK_THDH2_ALS_REG);
  1340. if(als_thd_h0_reg < 0)
  1341. {
  1342. printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_h0_reg);
  1343. return -EINVAL;
  1344. }
  1345. if(als_thd_h1_reg < 0)
  1346. {
  1347. printk(KERN_ERR "%s fail, err=0x%x", __func__, als_thd_h1_reg);
  1348. return -EINVAL;
  1349. }
  1350. als_thd_h0_reg|=(als_thd_h1_reg<<8);
  1351. als_lux = stk_alscode2lux(ps_data, als_thd_h0_reg);
  1352. mutex_unlock(&ps_data->io_lock);
  1353. return scnprintf(buf, PAGE_SIZE, "%d\n", als_lux);
  1354. }
  1355. static ssize_t stk_als_lux_thd_h_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  1356. {
  1357. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1358. unsigned long value = 0;
  1359. int ret;
  1360. ret = strict_strtoul(buf, 10, &value);
  1361. if(ret < 0)
  1362. {
  1363. printk(KERN_ERR "%s:strict_strtoul failed, ret=0x%x\n", __func__, ret);
  1364. return ret;
  1365. }
  1366. mutex_lock(&ps_data->io_lock);
  1367. value = stk_lux2alscode(ps_data, value);
  1368. stk3x1x_set_als_thd_h(ps_data, value);
  1369. mutex_unlock(&ps_data->io_lock);
  1370. return size;
  1371. }
  1372. #endif
  1373. static ssize_t stk_all_reg_show(struct device *dev, struct device_attribute *attr, char *buf)
  1374. {
  1375. int32_t ps_reg[27];
  1376. uint8_t cnt;
  1377. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1378. mutex_lock(&ps_data->io_lock);
  1379. for(cnt=0;cnt<25;cnt++)
  1380. {
  1381. ps_reg[cnt] = i2c_smbus_read_byte_data(ps_data->client, (cnt));
  1382. if(ps_reg[cnt] < 0)
  1383. {
  1384. mutex_unlock(&ps_data->io_lock);
  1385. printk(KERN_ERR "stk_all_reg_show:i2c_smbus_read_byte_data fail, ret=%d", ps_reg[cnt]);
  1386. return -EINVAL;
  1387. }
  1388. else
  1389. {
  1390. dev_dbg(dev, "reg[0x%2X]=0x%2X\n", cnt, ps_reg[cnt]);
  1391. }
  1392. }
  1393. ps_reg[cnt] = i2c_smbus_read_byte_data(ps_data->client, STK_PDT_ID_REG);
  1394. if(ps_reg[cnt] < 0)
  1395. {
  1396. mutex_unlock(&ps_data->io_lock);
  1397. printk( KERN_ERR "all_reg_show:i2c_smbus_read_byte_data fail, ret=%d", ps_reg[cnt]);
  1398. return -EINVAL;
  1399. }
  1400. dev_dbg(dev, "reg[0x%x]=0x%2X\n", STK_PDT_ID_REG, ps_reg[cnt]);
  1401. cnt++;
  1402. ps_reg[cnt] = i2c_smbus_read_byte_data(ps_data->client, STK_RSRVD_REG);
  1403. if(ps_reg[cnt] < 0)
  1404. {
  1405. mutex_unlock(&ps_data->io_lock);
  1406. printk( KERN_ERR "all_reg_show:i2c_smbus_read_byte_data fail, ret=%d", ps_reg[cnt]);
  1407. return -EINVAL;
  1408. }
  1409. dev_dbg(dev, "reg[0x%x]=0x%2X\n", STK_RSRVD_REG, ps_reg[cnt]);
  1410. mutex_unlock(&ps_data->io_lock);
  1411. return scnprintf(buf, PAGE_SIZE, "%2X %2X %2X %2X %2X,%2X %2X %2X %2X %2X,%2X %2X %2X %2X %2X,%2X %2X %2X %2X %2X,%2X %2X %2X %2X %2X,%2X %2X\n",
  1412. ps_reg[0], ps_reg[1], ps_reg[2], ps_reg[3], ps_reg[4], ps_reg[5], ps_reg[6], ps_reg[7], ps_reg[8],
  1413. ps_reg[9], ps_reg[10], ps_reg[11], ps_reg[12], ps_reg[13], ps_reg[14], ps_reg[15], ps_reg[16], ps_reg[17],
  1414. ps_reg[18], ps_reg[19], ps_reg[20], ps_reg[21], ps_reg[22], ps_reg[23], ps_reg[24], ps_reg[25], ps_reg[26]);
  1415. }
  1416. static ssize_t stk_recv_show(struct device *dev, struct device_attribute *attr, char *buf)
  1417. {
  1418. return 0;
  1419. }
  1420. static ssize_t stk_recv_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  1421. {
  1422. unsigned long value = 0;
  1423. int ret;
  1424. int32_t recv_data;
  1425. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1426. ret = kstrtoul(buf, 16, &value);
  1427. if (ret < 0) {
  1428. printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
  1429. __func__, ret);
  1430. return ret;
  1431. }
  1432. recv_data = i2c_smbus_read_byte_data(ps_data->client,value);
  1433. printk("%s: reg 0x%x=0x%x\n", __func__, (int)value, recv_data);
  1434. return size;
  1435. }
  1436. static ssize_t stk_send_show(struct device *dev, struct device_attribute *attr, char *buf)
  1437. {
  1438. return 0;
  1439. }
  1440. static ssize_t stk_send_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)
  1441. {
  1442. int addr, cmd;
  1443. u8 addr_u8, cmd_u8;
  1444. int32_t ret, i;
  1445. char *token[10];
  1446. struct stk3x1x_data *ps_data = dev_get_drvdata(dev);
  1447. for (i = 0; i < 2; i++)
  1448. token[i] = strsep((char **)&buf, " ");
  1449. ret = kstrtoul(token[0], 16, (unsigned long *)&(addr));
  1450. if (ret < 0) {
  1451. printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
  1452. __func__, ret);
  1453. return ret;
  1454. }
  1455. ret = kstrtoul(token[1], 16, (unsigned long *)&(cmd));
  1456. if (ret < 0) {
  1457. printk(KERN_ERR "%s:kstrtoul failed, ret=0x%x\n",
  1458. __func__, ret);
  1459. return ret;
  1460. }
  1461. dev_dbg(dev, "%s: write reg 0x%x=0x%x\n", __func__, addr, cmd);
  1462. addr_u8 = (u8) addr;
  1463. cmd_u8 = (u8) cmd;
  1464. //mutex_lock(&ps_data->io_lock);
  1465. ret = i2c_smbus_write_byte_data(ps_data->client,addr_u8,cmd_u8);
  1466. //mutex_unlock(&ps_data->io_lock);
  1467. if (0 != ret)
  1468. {
  1469. printk(KERN_ERR "%s: i2c_smbus_write_byte_data fail\n", __func__);
  1470. return ret;
  1471. }
  1472. return size;
  1473. }
  1474. static struct device_attribute als_enable_attribute = __ATTR(enable,0664,stk_als_enable_show,stk_als_enable_store);
  1475. static struct device_attribute als_lux_attribute = __ATTR(lux,0664,stk_als_lux_show,stk_als_lux_store);
  1476. static struct device_attribute als_code_attribute = __ATTR(code, 0444, stk_als_code_show, NULL);
  1477. static struct device_attribute als_transmittance_attribute = __ATTR(transmittance,0664,stk_als_transmittance_show,stk_als_transmittance_store);
  1478. static struct device_attribute als_poll_delay_attribute =
  1479. __ATTR(poll_delay, 0664, stk_als_delay_show, stk_als_delay_store);
  1480. static struct device_attribute als_ir_code_attribute = __ATTR(ircode,0444,stk_als_ir_code_show,NULL);
  1481. static struct device_attribute als_firlen_attribute =
  1482. __ATTR(firlen, 0664, stk_als_firlen_show, stk_als_firlen_store);
  1483. static struct device_attribute als_fir_enable_attribute =
  1484. __ATTR(fir_enable, 0664, stk_als_fir_enable_show,
  1485. stk_als_fir_enable_store);
  1486. static struct attribute *stk_als_attrs [] =
  1487. {
  1488. &als_enable_attribute.attr,
  1489. &als_lux_attribute.attr,
  1490. &als_code_attribute.attr,
  1491. &als_transmittance_attribute.attr,
  1492. &als_poll_delay_attribute.attr,
  1493. &als_ir_code_attribute.attr,
  1494. &als_firlen_attribute.attr,
  1495. &als_fir_enable_attribute.attr,
  1496. NULL
  1497. };
  1498. static struct attribute_group stk_als_attribute_group = {
  1499. .attrs = stk_als_attrs,
  1500. };
  1501. static struct device_attribute ps_enable_attribute = __ATTR(enable,0664,stk_ps_enable_show,stk_ps_enable_store);
  1502. static struct device_attribute ps_enable_aso_attribute = __ATTR(enableaso,0664,stk_ps_enable_aso_show,stk_ps_enable_aso_store);
  1503. static struct device_attribute ps_distance_attribute = __ATTR(distance,0664,stk_ps_distance_show, stk_ps_distance_store);
  1504. static struct device_attribute ps_offset_attribute = __ATTR(offset,0664,stk_ps_offset_show, stk_ps_offset_store);
  1505. static struct device_attribute ps_code_attribute = __ATTR(code, 0444, stk_ps_code_show, NULL);
  1506. static struct device_attribute ps_code_thd_l_attribute = __ATTR(codethdl,0664,stk_ps_code_thd_l_show,stk_ps_code_thd_l_store);
  1507. static struct device_attribute ps_code_thd_h_attribute = __ATTR(codethdh,0664,stk_ps_code_thd_h_show,stk_ps_code_thd_h_store);
  1508. static struct device_attribute recv_attribute = __ATTR(recv,0664,stk_recv_show,stk_recv_store);
  1509. static struct device_attribute send_attribute = __ATTR(send,0664,stk_send_show, stk_send_store);
  1510. static struct device_attribute all_reg_attribute = __ATTR(allreg, 0444, stk_all_reg_show, NULL);
  1511. static struct attribute *stk_ps_attrs [] =
  1512. {
  1513. &ps_enable_attribute.attr,
  1514. &ps_enable_aso_attribute.attr,
  1515. &ps_distance_attribute.attr,
  1516. &ps_offset_attribute.attr,
  1517. &ps_code_attribute.attr,
  1518. &ps_code_thd_l_attribute.attr,
  1519. &ps_code_thd_h_attribute.attr,
  1520. &recv_attribute.attr,
  1521. &send_attribute.attr,
  1522. &all_reg_attribute.attr,
  1523. NULL
  1524. };
  1525. static struct attribute_group stk_ps_attribute_group = {
  1526. .attrs = stk_ps_attrs,
  1527. };
  1528. #ifdef STK_POLL_ALS
  1529. static enum hrtimer_restart stk_als_timer_func(struct hrtimer *timer)
  1530. {
  1531. struct stk3x1x_data *ps_data = container_of(timer, struct stk3x1x_data, als_timer);
  1532. queue_work(ps_data->stk_als_wq, &ps_data->stk_als_work);
  1533. hrtimer_forward_now(&ps_data->als_timer, ps_data->als_poll_delay);
  1534. return HRTIMER_RESTART;
  1535. }
  1536. static void stk_als_work_func(struct work_struct *work)
  1537. {
  1538. struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_als_work);
  1539. int32_t reading;
  1540. mutex_lock(&ps_data->io_lock);
  1541. reading = stk3x1x_get_als_reading(ps_data);
  1542. if(reading < 0)
  1543. return;
  1544. ps_data->als_lux_last = stk_alscode2lux(ps_data, reading);
  1545. input_report_abs(ps_data->als_input_dev, ABS_MISC, ps_data->als_lux_last);
  1546. input_sync(ps_data->als_input_dev);
  1547. mutex_unlock(&ps_data->io_lock);
  1548. }
  1549. #endif
  1550. static enum hrtimer_restart stk_ps_timer_func(struct hrtimer *timer)
  1551. {
  1552. struct stk3x1x_data *ps_data = container_of(timer, struct stk3x1x_data, ps_timer);
  1553. queue_work(ps_data->stk_ps_wq, &ps_data->stk_ps_work);
  1554. #ifdef STK_POLL_PS
  1555. hrtimer_forward_now(&ps_data->ps_timer, ps_data->ps_poll_delay);
  1556. return HRTIMER_RESTART;
  1557. #else
  1558. hrtimer_cancel(&ps_data->ps_timer);
  1559. return HRTIMER_NORESTART;
  1560. #endif
  1561. }
  1562. static void stk_ps_work_func(struct work_struct *work)
  1563. {
  1564. struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_ps_work);
  1565. uint32_t reading;
  1566. int32_t near_far_state;
  1567. uint8_t org_flag_reg;
  1568. int32_t ret;
  1569. uint8_t disable_flag = 0;
  1570. mutex_lock(&ps_data->io_lock);
  1571. org_flag_reg = stk3x1x_get_flag(ps_data);
  1572. if(org_flag_reg < 0)
  1573. {
  1574. printk(KERN_ERR "%s: get_status_reg fail, ret=%d", __func__, org_flag_reg);
  1575. goto err_i2c_rw;
  1576. }
  1577. near_far_state = (org_flag_reg & STK_FLG_NF_MASK)?1:0;
  1578. reading = stk3x1x_get_ps_reading(ps_data);
  1579. if(ps_data->ps_distance_last != near_far_state)
  1580. {
  1581. ps_data->ps_distance_last = near_far_state;
  1582. input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
  1583. input_sync(ps_data->ps_input_dev);
  1584. wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
  1585. #ifdef STK_DEBUG_PRINTF
  1586. printk(KERN_INFO "%s: ps input event %d cm, ps code = %d\n",__func__, near_far_state, reading);
  1587. #endif
  1588. }
  1589. ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag);
  1590. if(ret < 0)
  1591. {
  1592. printk(KERN_ERR "%s:stk3x1x_set_flag fail, ret=%d\n", __func__, ret);
  1593. goto err_i2c_rw;
  1594. }
  1595. mutex_unlock(&ps_data->io_lock);
  1596. return;
  1597. err_i2c_rw:
  1598. mutex_unlock(&ps_data->io_lock);
  1599. msleep(30);
  1600. return;
  1601. }
  1602. #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
  1603. static void stk_work_func(struct work_struct *work)
  1604. {
  1605. uint32_t reading;
  1606. #if ((STK_INT_PS_MODE != 0x03) && (STK_INT_PS_MODE != 0x02))
  1607. int32_t ret;
  1608. uint8_t disable_flag = 0;
  1609. uint8_t org_flag_reg;
  1610. #endif /* #if ((STK_INT_PS_MODE != 0x03) && (STK_INT_PS_MODE != 0x02)) */
  1611. #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
  1612. uint32_t nLuxIndex;
  1613. #endif
  1614. struct stk3x1x_data *ps_data = container_of(work, struct stk3x1x_data, stk_work);
  1615. int32_t near_far_state;
  1616. mutex_lock(&ps_data->io_lock);
  1617. #if (STK_INT_PS_MODE == 0x03)
  1618. near_far_state = gpio_get_value(ps_data->int_pin);
  1619. #elif (STK_INT_PS_MODE == 0x02)
  1620. near_far_state = !(gpio_get_value(ps_data->int_pin));
  1621. #endif
  1622. #if ((STK_INT_PS_MODE == 0x03) || (STK_INT_PS_MODE == 0x02))
  1623. ps_data->ps_distance_last = near_far_state;
  1624. input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
  1625. input_sync(ps_data->ps_input_dev);
  1626. wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
  1627. reading = stk3x1x_get_ps_reading(ps_data);
  1628. #ifdef STK_DEBUG_PRINTF
  1629. printk(KERN_INFO "%s: ps input event %d cm, ps code = %d\n",__func__, near_far_state, reading);
  1630. #endif
  1631. #else
  1632. /* mode 0x01 or 0x04 */
  1633. org_flag_reg = stk3x1x_get_flag(ps_data);
  1634. if(org_flag_reg < 0)
  1635. {
  1636. printk(KERN_ERR "%s: get_status_reg fail, org_flag_reg=%d", __func__, org_flag_reg);
  1637. goto err_i2c_rw;
  1638. }
  1639. if (org_flag_reg & STK_FLG_ALSINT_MASK)
  1640. {
  1641. disable_flag |= STK_FLG_ALSINT_MASK;
  1642. reading = stk3x1x_get_als_reading(ps_data);
  1643. if(reading < 0)
  1644. {
  1645. printk(KERN_ERR "%s: stk3x1x_get_als_reading fail, ret=%d", __func__, reading);
  1646. goto err_i2c_rw;
  1647. }
  1648. #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
  1649. nLuxIndex = stk_get_lux_interval_index(reading);
  1650. stk3x1x_set_als_thd_h(ps_data, code_threshold_table[nLuxIndex]);
  1651. stk3x1x_set_als_thd_l(ps_data, code_threshold_table[nLuxIndex-1]);
  1652. #else
  1653. stk_als_set_new_thd(ps_data, reading);
  1654. #endif //CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
  1655. ps_data->als_lux_last = stk_alscode2lux(ps_data, reading);
  1656. input_report_abs(ps_data->als_input_dev, ABS_MISC, ps_data->als_lux_last);
  1657. input_sync(ps_data->als_input_dev);
  1658. #ifdef STK_DEBUG_PRINTF
  1659. printk(KERN_INFO "%s: als input event %d lux\n",__func__, ps_data->als_lux_last);
  1660. #endif
  1661. }
  1662. if (org_flag_reg & STK_FLG_PSINT_MASK)
  1663. {
  1664. disable_flag |= STK_FLG_PSINT_MASK;
  1665. near_far_state = (org_flag_reg & STK_FLG_NF_MASK)?1:0;
  1666. ps_data->ps_distance_last = near_far_state;
  1667. input_report_abs(ps_data->ps_input_dev, ABS_DISTANCE, near_far_state);
  1668. input_sync(ps_data->ps_input_dev);
  1669. wake_lock_timeout(&ps_data->ps_wakelock, 3*HZ);
  1670. reading = stk3x1x_get_ps_reading(ps_data);
  1671. #ifdef STK_DEBUG_PRINTF
  1672. printk(KERN_INFO "%s: ps input event=%d, ps code = %d\n",__func__, near_far_state, reading);
  1673. #endif
  1674. }
  1675. ret = stk3x1x_set_flag(ps_data, org_flag_reg, disable_flag);
  1676. if(ret < 0)
  1677. {
  1678. printk(KERN_ERR "%s:reset_int_flag fail, ret=%d\n", __func__, ret);
  1679. goto err_i2c_rw;
  1680. }
  1681. #endif
  1682. msleep(1);
  1683. enable_irq(ps_data->irq);
  1684. mutex_unlock(&ps_data->io_lock);
  1685. return;
  1686. err_i2c_rw:
  1687. mutex_unlock(&ps_data->io_lock);
  1688. msleep(30);
  1689. enable_irq(ps_data->irq);
  1690. return;
  1691. }
  1692. #endif
  1693. #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
  1694. static irqreturn_t stk_oss_irq_handler(int irq, void *data)
  1695. {
  1696. struct stk3x1x_data *pData = data;
  1697. disable_irq_nosync(irq);
  1698. queue_work(pData->stk_wq,&pData->stk_work);
  1699. return IRQ_HANDLED;
  1700. }
  1701. #endif /* #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS)) */
  1702. static inline void stk3x1x_init_fir(struct stk3x1x_data *ps_data)
  1703. {
  1704. memset(&ps_data->fir, 0x00, sizeof(ps_data->fir));
  1705. atomic_set(&ps_data->firlength, STK_FIR_LEN);
  1706. }
  1707. static int32_t stk3x1x_init_all_setting(struct i2c_client *client, struct stk3x1x_platform_data *plat_data)
  1708. {
  1709. int32_t ret;
  1710. struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
  1711. ret = stk3x1x_software_reset(ps_data);
  1712. if(ret < 0)
  1713. return ret;
  1714. stk3x1x_check_pid(ps_data);
  1715. if(ret < 0)
  1716. return ret;
  1717. ret = stk3x1x_init_all_reg(ps_data, plat_data);
  1718. if(ret < 0)
  1719. return ret;
  1720. #ifndef CONFIG_STK_PS_ALS_USE_CHANGE_THRESHOLD
  1721. stk_init_code_threshold_table(ps_data);
  1722. #endif
  1723. if (plat_data->use_fir)
  1724. stk3x1x_init_fir(ps_data);
  1725. return 0;
  1726. }
  1727. #if (!defined(STK_POLL_PS) || !defined(STK_POLL_ALS))
  1728. static int stk3x1x_setup_irq(struct i2c_client *client)
  1729. {
  1730. int irq, err = -EIO;
  1731. struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
  1732. irq = gpio_to_irq(ps_data->int_pin);
  1733. #ifdef STK_DEBUG_PRINTF
  1734. printk(KERN_INFO "%s: int pin #=%d, irq=%d\n",__func__, ps_data->int_pin, irq);
  1735. #endif
  1736. if (irq <= 0)
  1737. {
  1738. printk(KERN_ERR "irq number is not specified, irq # = %d, int pin=%d\n",irq, ps_data->int_pin);
  1739. return irq;
  1740. }
  1741. ps_data->irq = irq;
  1742. err = gpio_request(ps_data->int_pin,"stk-int");
  1743. if(err < 0)
  1744. {
  1745. printk(KERN_ERR "%s: gpio_request, err=%d", __func__, err);
  1746. return err;
  1747. }
  1748. err = gpio_direction_input(ps_data->int_pin);
  1749. if(err < 0)
  1750. {
  1751. printk(KERN_ERR "%s: gpio_direction_input, err=%d", __func__, err);
  1752. return err;
  1753. }
  1754. #if ((STK_INT_PS_MODE == 0x03) || (STK_INT_PS_MODE == 0x02))
  1755. err = request_any_context_irq(irq, stk_oss_irq_handler, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, DEVICE_NAME, ps_data);
  1756. #else
  1757. err = request_any_context_irq(irq, stk_oss_irq_handler, IRQF_TRIGGER_LOW, DEVICE_NAME, ps_data);
  1758. #endif
  1759. if (err < 0)
  1760. {
  1761. printk(KERN_WARNING "%s: request_any_context_irq(%d) failed for (%d)\n", __func__, irq, err);
  1762. goto err_request_any_context_irq;
  1763. }
  1764. disable_irq(irq);
  1765. return 0;
  1766. err_request_any_context_irq:
  1767. gpio_free(ps_data->int_pin);
  1768. return err;
  1769. }
  1770. #endif
  1771. #ifdef CONFIG_HAS_EARLYSUSPEND
  1772. static void stk3x1x_early_suspend(struct early_suspend *h)
  1773. {
  1774. struct stk3x1x_data *ps_data = container_of(h, struct stk3x1x_data, stk_early_suspend);
  1775. #ifndef STK_POLL_PS
  1776. int err;
  1777. #endif
  1778. mutex_lock(&ps_data->io_lock);
  1779. if(ps_data->als_enabled)
  1780. {
  1781. stk3x1x_enable_als(ps_data, 0);
  1782. ps_data->als_enabled = true;
  1783. }
  1784. if(ps_data->ps_enabled)
  1785. {
  1786. #ifdef STK_POLL_PS
  1787. wake_lock(&ps_data->ps_nosuspend_wl);
  1788. #else
  1789. err = enable_irq_wake(ps_data->irq);
  1790. if (err)
  1791. printk(KERN_WARNING "%s: set_irq_wake(%d) failed, err=(%d)\n", __func__, ps_data->irq, err);
  1792. #endif
  1793. }
  1794. mutex_unlock(&ps_data->io_lock);
  1795. return;
  1796. }
  1797. static void stk3x1x_late_resume(struct early_suspend *h)
  1798. {
  1799. struct stk3x1x_data *ps_data = container_of(h, struct stk3x1x_data, stk_early_suspend);
  1800. #ifndef STK_POLL_PS
  1801. int err;
  1802. #endif
  1803. mutex_lock(&ps_data->io_lock);
  1804. if(ps_data->als_enabled)
  1805. stk3x1x_enable_als(ps_data, 1);
  1806. if(ps_data->ps_enabled)
  1807. {
  1808. #ifdef STK_POLL_PS
  1809. wake_lock(&ps_data->ps_nosuspend_wl);
  1810. #else
  1811. err = disable_irq_wake(ps_data->irq);
  1812. if (err)
  1813. printk(KERN_WARNING "%s: disable_irq_wake(%d) failed, err=(%d)\n", __func__, ps_data->irq, err);
  1814. #endif
  1815. }
  1816. mutex_unlock(&ps_data->io_lock);
  1817. return;
  1818. }
  1819. #endif //#ifdef CONFIG_HAS_EARLYSUSPEND
  1820. static int stk3x1x_power_ctl(struct stk3x1x_data *data, bool on)
  1821. {
  1822. int ret = 0;
  1823. if (!on && data->power_enabled) {
  1824. ret = regulator_disable(data->vdd);
  1825. if (ret) {
  1826. dev_err(&data->client->dev,
  1827. "Regulator vdd disable failed ret=%d\n", ret);
  1828. return ret;
  1829. }
  1830. ret = regulator_disable(data->vio);
  1831. if (ret) {
  1832. dev_err(&data->client->dev,
  1833. "Regulator vio disable failed ret=%d\n", ret);
  1834. regulator_enable(data->vdd);
  1835. return ret;
  1836. }
  1837. data->power_enabled = on;
  1838. dev_dbg(&data->client->dev, "stk3x1x_power_ctl on=%d\n",
  1839. on);
  1840. } else if (on && !data->power_enabled) {
  1841. ret = regulator_enable(data->vdd);
  1842. if (ret) {
  1843. dev_err(&data->client->dev,
  1844. "Regulator vdd enable failed ret=%d\n", ret);
  1845. return ret;
  1846. }
  1847. ret = regulator_enable(data->vio);
  1848. if (ret) {
  1849. dev_err(&data->client->dev,
  1850. "Regulator vio enable failed ret=%d\n", ret);
  1851. regulator_disable(data->vdd);
  1852. return ret;
  1853. }
  1854. data->power_enabled = on;
  1855. dev_dbg(&data->client->dev, "stk3x1x_power_ctl on=%d\n",
  1856. on);
  1857. } else {
  1858. dev_warn(&data->client->dev,
  1859. "Power on=%d. enabled=%d\n",
  1860. on, data->power_enabled);
  1861. }
  1862. return ret;
  1863. }
  1864. static int stk3x1x_power_init(struct stk3x1x_data *data, bool on)
  1865. {
  1866. int ret;
  1867. if (!on) {
  1868. if (regulator_count_voltages(data->vdd) > 0)
  1869. regulator_set_voltage(data->vdd,
  1870. 0, STK3X1X_VDD_MAX_UV);
  1871. regulator_put(data->vdd);
  1872. if (regulator_count_voltages(data->vio) > 0)
  1873. regulator_set_voltage(data->vio,
  1874. 0, STK3X1X_VIO_MAX_UV);
  1875. regulator_put(data->vio);
  1876. } else {
  1877. data->vdd = regulator_get(&data->client->dev, "vdd");
  1878. if (IS_ERR(data->vdd)) {
  1879. ret = PTR_ERR(data->vdd);
  1880. dev_err(&data->client->dev,
  1881. "Regulator get failed vdd ret=%d\n", ret);
  1882. return ret;
  1883. }
  1884. if (regulator_count_voltages(data->vdd) > 0) {
  1885. ret = regulator_set_voltage(data->vdd,
  1886. STK3X1X_VDD_MIN_UV,
  1887. STK3X1X_VDD_MAX_UV);
  1888. if (ret) {
  1889. dev_err(&data->client->dev,
  1890. "Regulator set failed vdd ret=%d\n",
  1891. ret);
  1892. goto reg_vdd_put;
  1893. }
  1894. }
  1895. data->vio = regulator_get(&data->client->dev, "vio");
  1896. if (IS_ERR(data->vio)) {
  1897. ret = PTR_ERR(data->vio);
  1898. dev_err(&data->client->dev,
  1899. "Regulator get failed vio ret=%d\n", ret);
  1900. goto reg_vdd_set;
  1901. }
  1902. if (regulator_count_voltages(data->vio) > 0) {
  1903. ret = regulator_set_voltage(data->vio,
  1904. STK3X1X_VIO_MIN_UV,
  1905. STK3X1X_VIO_MAX_UV);
  1906. if (ret) {
  1907. dev_err(&data->client->dev,
  1908. "Regulator set failed vio ret=%d\n", ret);
  1909. goto reg_vio_put;
  1910. }
  1911. }
  1912. }
  1913. return 0;
  1914. reg_vio_put:
  1915. regulator_put(data->vio);
  1916. reg_vdd_set:
  1917. if (regulator_count_voltages(data->vdd) > 0)
  1918. regulator_set_voltage(data->vdd, 0, STK3X1X_VDD_MAX_UV);
  1919. reg_vdd_put:
  1920. regulator_put(data->vdd);
  1921. return ret;
  1922. }
  1923. static int stk3x1x_device_ctl(struct stk3x1x_data *ps_data, bool enable)
  1924. {
  1925. int ret;
  1926. struct device *dev = &ps_data->client->dev;
  1927. if (enable && !ps_data->power_enabled) {
  1928. ret = stk3x1x_power_ctl(ps_data, true);
  1929. if (ret) {
  1930. dev_err(dev, "Failed to enable device power\n");
  1931. goto err_exit;
  1932. }
  1933. ret = stk3x1x_init_all_setting(ps_data->client, ps_data->pdata);
  1934. if (ret < 0) {
  1935. stk3x1x_power_ctl(ps_data, false);
  1936. dev_err(dev, "Failed to re-init device setting\n");
  1937. goto err_exit;
  1938. }
  1939. } else if (!enable && ps_data->power_enabled) {
  1940. if (!ps_data->als_enabled && !ps_data->ps_enabled) {
  1941. ret = stk3x1x_power_ctl(ps_data, false);
  1942. if (ret) {
  1943. dev_err(dev, "Failed to disable device power\n");
  1944. goto err_exit;
  1945. }
  1946. } else {
  1947. dev_dbg(dev, "device control: als_enabled=%d, ps_enabled=%d\n",
  1948. ps_data->als_enabled, ps_data->ps_enabled);
  1949. }
  1950. } else {
  1951. dev_dbg(dev, "device control: enable=%d, power_enabled=%d\n",
  1952. enable, ps_data->power_enabled);
  1953. }
  1954. return 0;
  1955. err_exit:
  1956. return ret;
  1957. }
  1958. #ifdef CONFIG_OF
  1959. static int stk3x1x_parse_dt(struct device *dev,
  1960. struct stk3x1x_platform_data *pdata)
  1961. {
  1962. int rc;
  1963. struct device_node *np = dev->of_node;
  1964. u32 temp_val;
  1965. pdata->int_pin = of_get_named_gpio_flags(np, "stk,irq-gpio",
  1966. 0, &pdata->int_flags);
  1967. if (pdata->int_pin < 0) {
  1968. dev_err(dev, "Unable to read irq-gpio\n");
  1969. return pdata->int_pin;
  1970. }
  1971. rc = of_property_read_u32(np, "stk,transmittance", &temp_val);
  1972. if (!rc)
  1973. pdata->transmittance = temp_val;
  1974. else {
  1975. dev_err(dev, "Unable to read transmittance\n");
  1976. return rc;
  1977. }
  1978. rc = of_property_read_u32(np, "stk,state-reg", &temp_val);
  1979. if (!rc)
  1980. pdata->state_reg = temp_val;
  1981. else {
  1982. dev_err(dev, "Unable to read state-reg\n");
  1983. return rc;
  1984. }
  1985. rc = of_property_read_u32(np, "stk,psctrl-reg", &temp_val);
  1986. if (!rc)
  1987. pdata->psctrl_reg = (u8)temp_val;
  1988. else {
  1989. dev_err(dev, "Unable to read psctrl-reg\n");
  1990. return rc;
  1991. }
  1992. rc = of_property_read_u32(np, "stk,alsctrl-reg", &temp_val);
  1993. if (!rc)
  1994. pdata->alsctrl_reg = (u8)temp_val;
  1995. else {
  1996. dev_err(dev, "Unable to read alsctrl-reg\n");
  1997. return rc;
  1998. }
  1999. rc = of_property_read_u32(np, "stk,ledctrl-reg", &temp_val);
  2000. if (!rc)
  2001. pdata->ledctrl_reg = (u8)temp_val;
  2002. else {
  2003. dev_err(dev, "Unable to read ledctrl-reg\n");
  2004. return rc;
  2005. }
  2006. rc = of_property_read_u32(np, "stk,wait-reg", &temp_val);
  2007. if (!rc)
  2008. pdata->wait_reg = (u8)temp_val;
  2009. else {
  2010. dev_err(dev, "Unable to read wait-reg\n");
  2011. return rc;
  2012. }
  2013. rc = of_property_read_u32(np, "stk,ps-thdh", &temp_val);
  2014. if (!rc)
  2015. pdata->ps_thd_h = (u16)temp_val;
  2016. else {
  2017. dev_err(dev, "Unable to read ps-thdh\n");
  2018. return rc;
  2019. }
  2020. rc = of_property_read_u32(np, "stk,ps-thdl", &temp_val);
  2021. if (!rc)
  2022. pdata->ps_thd_l = (u16)temp_val;
  2023. else {
  2024. dev_err(dev, "Unable to read ps-thdl\n");
  2025. return rc;
  2026. }
  2027. pdata->use_fir = of_property_read_bool(np, "stk,use-fir");
  2028. return 0;
  2029. }
  2030. #else
  2031. static int stk3x1x_parse_dt(struct device *dev,
  2032. struct stk3x1x_platform_data *pdata)
  2033. {
  2034. return -ENODEV;
  2035. }
  2036. #endif /* !CONFIG_OF */
  2037. static int stk3x1x_probe(struct i2c_client *client,
  2038. const struct i2c_device_id *id)
  2039. {
  2040. int err = -ENODEV;
  2041. struct stk3x1x_data *ps_data;
  2042. struct stk3x1x_platform_data *plat_data;
  2043. printk(KERN_INFO "%s: driver version = %s\n", __func__, DRIVER_VERSION);
  2044. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  2045. {
  2046. printk(KERN_ERR "%s: No Support for I2C_FUNC_SMBUS_BYTE_DATA\n", __func__);
  2047. return -ENODEV;
  2048. }
  2049. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
  2050. {
  2051. printk(KERN_ERR "%s: No Support for I2C_FUNC_SMBUS_WORD_DATA\n", __func__);
  2052. return -ENODEV;
  2053. }
  2054. ps_data = kzalloc(sizeof(struct stk3x1x_data),GFP_KERNEL);
  2055. if(!ps_data)
  2056. {
  2057. printk(KERN_ERR "%s: failed to allocate stk3x1x_data\n", __func__);
  2058. return -ENOMEM;
  2059. }
  2060. ps_data->client = client;
  2061. i2c_set_clientdata(client,ps_data);
  2062. mutex_init(&ps_data->io_lock);
  2063. wake_lock_init(&ps_data->ps_wakelock,WAKE_LOCK_SUSPEND, "stk_input_wakelock");
  2064. #ifdef STK_POLL_PS
  2065. wake_lock_init(&ps_data->ps_nosuspend_wl,WAKE_LOCK_SUSPEND, "stk_nosuspend_wakelock");
  2066. #endif
  2067. if (client->dev.of_node) {
  2068. plat_data = devm_kzalloc(&client->dev,
  2069. sizeof(struct stk3x1x_platform_data), GFP_KERNEL);
  2070. if (!plat_data) {
  2071. dev_err(&client->dev, "Failed to allocate memory\n");
  2072. return -ENOMEM;
  2073. }
  2074. err = stk3x1x_parse_dt(&client->dev, plat_data);
  2075. dev_err(&client->dev,
  2076. "%s: stk3x1x_parse_dt ret=%d\n", __func__, err);
  2077. if (err)
  2078. return err;
  2079. } else
  2080. plat_data = client->dev.platform_data;
  2081. if (!plat_data) {
  2082. dev_err(&client->dev,
  2083. "%s: no stk3x1x platform data!\n", __func__);
  2084. goto err_als_input_allocate;
  2085. }
  2086. ps_data->als_transmittance = plat_data->transmittance;
  2087. ps_data->int_pin = plat_data->int_pin;
  2088. ps_data->use_fir = plat_data->use_fir;
  2089. ps_data->pdata = plat_data;
  2090. if (ps_data->als_transmittance == 0) {
  2091. dev_err(&client->dev,
  2092. "%s: Please set als_transmittance\n", __func__);
  2093. goto err_als_input_allocate;
  2094. }
  2095. ps_data->als_input_dev = input_allocate_device();
  2096. if (ps_data->als_input_dev==NULL)
  2097. {
  2098. printk(KERN_ERR "%s: could not allocate als device\n", __func__);
  2099. err = -ENOMEM;
  2100. goto err_als_input_allocate;
  2101. }
  2102. ps_data->ps_input_dev = input_allocate_device();
  2103. if (ps_data->ps_input_dev==NULL)
  2104. {
  2105. printk(KERN_ERR "%s: could not allocate ps device\n", __func__);
  2106. err = -ENOMEM;
  2107. goto err_ps_input_allocate;
  2108. }
  2109. ps_data->als_input_dev->name = ALS_NAME;
  2110. ps_data->ps_input_dev->name = PS_NAME;
  2111. set_bit(EV_ABS, ps_data->als_input_dev->evbit);
  2112. set_bit(EV_ABS, ps_data->ps_input_dev->evbit);
  2113. input_set_abs_params(ps_data->als_input_dev, ABS_MISC, 0, stk_alscode2lux(ps_data, (1<<16)-1), 0, 0);
  2114. input_set_abs_params(ps_data->ps_input_dev, ABS_DISTANCE, 0,1, 0, 0);
  2115. err = input_register_device(ps_data->als_input_dev);
  2116. if (err<0)
  2117. {
  2118. printk(KERN_ERR "%s: can not register als input device\n", __func__);
  2119. goto err_als_input_register;
  2120. }
  2121. err = input_register_device(ps_data->ps_input_dev);
  2122. if (err<0)
  2123. {
  2124. printk(KERN_ERR "%s: can not register ps input device\n", __func__);
  2125. goto err_ps_input_register;
  2126. }
  2127. err = sysfs_create_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group);
  2128. if (err < 0)
  2129. {
  2130. printk(KERN_ERR "%s:could not create sysfs group for als\n", __func__);
  2131. goto err_als_sysfs_create_group;
  2132. }
  2133. err = sysfs_create_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group);
  2134. if (err < 0)
  2135. {
  2136. printk(KERN_ERR "%s:could not create sysfs group for ps\n", __func__);
  2137. goto err_ps_sysfs_create_group;
  2138. }
  2139. input_set_drvdata(ps_data->als_input_dev, ps_data);
  2140. input_set_drvdata(ps_data->ps_input_dev, ps_data);
  2141. #ifdef STK_POLL_ALS
  2142. ps_data->stk_als_wq = create_singlethread_workqueue("stk_als_wq");
  2143. INIT_WORK(&ps_data->stk_als_work, stk_als_work_func);
  2144. hrtimer_init(&ps_data->als_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2145. ps_data->als_poll_delay = ns_to_ktime(110 * NSEC_PER_MSEC);
  2146. ps_data->als_timer.function = stk_als_timer_func;
  2147. #endif
  2148. ps_data->stk_ps_wq = create_singlethread_workqueue("stk_ps_wq");
  2149. INIT_WORK(&ps_data->stk_ps_work, stk_ps_work_func);
  2150. hrtimer_init(&ps_data->ps_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2151. ps_data->ps_poll_delay = ns_to_ktime(110 * NSEC_PER_MSEC);
  2152. ps_data->ps_timer.function = stk_ps_timer_func;
  2153. #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
  2154. ps_data->stk_wq = create_singlethread_workqueue("stk_wq");
  2155. INIT_WORK(&ps_data->stk_work, stk_work_func);
  2156. err = stk3x1x_setup_irq(client);
  2157. if(err < 0)
  2158. goto err_stk3x1x_setup_irq;
  2159. #endif
  2160. err = stk3x1x_power_init(ps_data, true);
  2161. if (err)
  2162. goto err_power_init;
  2163. err = stk3x1x_power_ctl(ps_data, true);
  2164. if (err)
  2165. goto err_power_on;
  2166. ps_data->als_enabled = false;
  2167. ps_data->ps_enabled = false;
  2168. #ifdef CONFIG_HAS_EARLYSUSPEND
  2169. ps_data->stk_early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  2170. ps_data->stk_early_suspend.suspend = stk3x1x_early_suspend;
  2171. ps_data->stk_early_suspend.resume = stk3x1x_late_resume;
  2172. register_early_suspend(&ps_data->stk_early_suspend);
  2173. #endif
  2174. /* make sure everything is ok before registering the class device */
  2175. ps_data->als_cdev = sensors_light_cdev;
  2176. ps_data->als_cdev.sensors_enable = stk_als_enable_set;
  2177. ps_data->als_cdev.sensors_poll_delay = stk_als_poll_delay_set;
  2178. err = sensors_classdev_register(&client->dev, &ps_data->als_cdev);
  2179. if (err)
  2180. goto err_power_on;
  2181. ps_data->ps_cdev = sensors_proximity_cdev;
  2182. ps_data->ps_cdev.sensors_enable = stk_ps_enable_set;
  2183. err = sensors_classdev_register(&client->dev, &ps_data->ps_cdev);
  2184. if (err)
  2185. goto err_class_sysfs;
  2186. /* enable device power only when it is enabled */
  2187. err = stk3x1x_power_ctl(ps_data, false);
  2188. if (err)
  2189. goto err_init_all_setting;
  2190. dev_dbg(&client->dev, "%s: probe successfully", __func__);
  2191. return 0;
  2192. err_init_all_setting:
  2193. stk3x1x_power_ctl(ps_data, false);
  2194. sensors_classdev_unregister(&ps_data->ps_cdev);
  2195. err_class_sysfs:
  2196. sensors_classdev_unregister(&ps_data->als_cdev);
  2197. err_power_on:
  2198. stk3x1x_power_init(ps_data, false);
  2199. err_power_init:
  2200. #ifndef STK_POLL_PS
  2201. free_irq(ps_data->irq, ps_data);
  2202. gpio_free(plat_data->int_pin);
  2203. #endif
  2204. #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
  2205. err_stk3x1x_setup_irq:
  2206. #endif
  2207. #ifdef STK_POLL_ALS
  2208. hrtimer_try_to_cancel(&ps_data->als_timer);
  2209. destroy_workqueue(ps_data->stk_als_wq);
  2210. #endif
  2211. destroy_workqueue(ps_data->stk_ps_wq);
  2212. #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
  2213. destroy_workqueue(ps_data->stk_wq);
  2214. #endif
  2215. sysfs_remove_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group);
  2216. err_ps_sysfs_create_group:
  2217. sysfs_remove_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group);
  2218. err_als_sysfs_create_group:
  2219. input_unregister_device(ps_data->ps_input_dev);
  2220. err_ps_input_register:
  2221. input_unregister_device(ps_data->als_input_dev);
  2222. err_als_input_register:
  2223. input_free_device(ps_data->ps_input_dev);
  2224. err_ps_input_allocate:
  2225. input_free_device(ps_data->als_input_dev);
  2226. err_als_input_allocate:
  2227. #ifdef STK_POLL_PS
  2228. wake_lock_destroy(&ps_data->ps_nosuspend_wl);
  2229. #endif
  2230. wake_lock_destroy(&ps_data->ps_wakelock);
  2231. mutex_destroy(&ps_data->io_lock);
  2232. kfree(ps_data);
  2233. return err;
  2234. }
  2235. static int stk3x1x_remove(struct i2c_client *client)
  2236. {
  2237. struct stk3x1x_data *ps_data = i2c_get_clientdata(client);
  2238. #ifndef STK_POLL_PS
  2239. free_irq(ps_data->irq, ps_data);
  2240. gpio_free(ps_data->int_pin);
  2241. #endif
  2242. #ifdef STK_POLL_ALS
  2243. hrtimer_try_to_cancel(&ps_data->als_timer);
  2244. destroy_workqueue(ps_data->stk_als_wq);
  2245. #endif
  2246. destroy_workqueue(ps_data->stk_ps_wq);
  2247. #if (!defined(STK_POLL_ALS) || !defined(STK_POLL_PS))
  2248. destroy_workqueue(ps_data->stk_wq);
  2249. #endif
  2250. sysfs_remove_group(&ps_data->ps_input_dev->dev.kobj, &stk_ps_attribute_group);
  2251. sysfs_remove_group(&ps_data->als_input_dev->dev.kobj, &stk_als_attribute_group);
  2252. input_unregister_device(ps_data->ps_input_dev);
  2253. input_unregister_device(ps_data->als_input_dev);
  2254. input_free_device(ps_data->ps_input_dev);
  2255. input_free_device(ps_data->als_input_dev);
  2256. #ifdef STK_POLL_PS
  2257. wake_lock_destroy(&ps_data->ps_nosuspend_wl);
  2258. #endif
  2259. wake_lock_destroy(&ps_data->ps_wakelock);
  2260. mutex_destroy(&ps_data->io_lock);
  2261. kfree(ps_data);
  2262. return 0;
  2263. }
  2264. static const struct i2c_device_id stk_ps_id[] =
  2265. {
  2266. { "stk_ps", 0},
  2267. {}
  2268. };
  2269. MODULE_DEVICE_TABLE(i2c, stk_ps_id);
  2270. static struct of_device_id stk_match_table[] = {
  2271. { .compatible = "stk,stk3x1x", },
  2272. { },
  2273. };
  2274. static struct i2c_driver stk_ps_driver =
  2275. {
  2276. .driver = {
  2277. .name = DEVICE_NAME,
  2278. .owner = THIS_MODULE,
  2279. .of_match_table = stk_match_table,
  2280. },
  2281. .probe = stk3x1x_probe,
  2282. .remove = stk3x1x_remove,
  2283. .id_table = stk_ps_id,
  2284. };
  2285. static int __init stk3x1x_init(void)
  2286. {
  2287. int ret;
  2288. ret = i2c_add_driver(&stk_ps_driver);
  2289. if (ret)
  2290. return ret;
  2291. return 0;
  2292. }
  2293. static void __exit stk3x1x_exit(void)
  2294. {
  2295. i2c_del_driver(&stk_ps_driver);
  2296. }
  2297. module_init(stk3x1x_init);
  2298. module_exit(stk3x1x_exit);
  2299. MODULE_AUTHOR("Lex Hsieh <lex_hsieh@sitronix.com.tw>");
  2300. MODULE_DESCRIPTION("Sensortek stk3x1x Proximity Sensor driver");
  2301. MODULE_LICENSE("GPL");
  2302. MODULE_VERSION(DRIVER_VERSION);