adpd142.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633
  1. /**
  2. *\mainpage
  3. * ADPD142 driver
  4. * (c) copyright 2014 Analog Devices Inc.
  5. * Licensed under the GPL version 3 or later.
  6. * \date April 2014
  7. * \version 1.1 Driver
  8. * \version Linux 3.4.0
  9. */
  10. #include "adpd142.h"
  11. #ifdef CONFIG_OF
  12. #include <linux/of_gpio.h>
  13. #endif
  14. static int dbg_enable;
  15. module_param(dbg_enable, int, S_IRUGO | S_IWUSR);
  16. /* ADPD142 driver version*/
  17. #define ADPD142_VERSION "1.2"
  18. /* ADPD142 release date*/
  19. #define ADPD142_RELEASE_DATE "Tue Apr 22 12:11:42 IST 2014"
  20. #define ADPD_DEV_NAME "adpd142"
  21. /*INPUT DEVICE NAME LIST*/
  22. #define MODULE_NAME_HRM "hrm_sensor"
  23. #define CHIP_NAME "ADPD142RI"
  24. #define VENDOR "ADI"
  25. #define MAX_EOL_RESULT 132
  26. /*I2C RELATED*/
  27. #define I2C_RETRY_DELAY 5
  28. #define I2C_RETRIES 5
  29. /*ADPD142 DRIVER RELATED*/
  30. #define MAX_BUFFER 128
  31. #define GLOBAL_OP_MODE_OFF 0
  32. #define GLOBAL_OP_MODE_IDLE 1
  33. #define GLOBAL_OP_MODE_GO 2
  34. #define SET_GLOBAL_OP_MODE(val, cmd) ((val & 0xFC) | cmd)
  35. #define FRM_FILE 0
  36. #define FRM_ARR 1
  37. #define EN_FIFO_CLK 0x3B5B
  38. #define DS_FIFO_CLK 0x3050
  39. /******************************************************************/
  40. /**************** ADPD142 USER OPERATING MODE ********************/
  41. /*
  42. Operating mode defined to user
  43. */
  44. #define IDLE_MODE 0x00
  45. #define SAMPLE_XY_A_MODE 0x30
  46. #define SAMPLE_XY_AB_MODE 0x31
  47. #define SAMPLE_XY_B_MODE 0x32
  48. #define GET_USR_MODE(usr_val) ((usr_val & 0xF0) >> 4)
  49. #define GET_USR_SUB_MODE(usr_val) (usr_val & 0xF)
  50. /******************************************************************/
  51. /**************** ADPD142 OPERATING MODE & DATA MODE *************/
  52. #define R_RDATA_FLAG_EN (0x1 << 15)
  53. #define R_PACK_START_EN (0x0 << 14)
  54. #define R_RDOUT_MODE (0x0 << 13)
  55. #define R_FIFO_PREVENT_EN (0x1 << 12)
  56. #define R_SAMP_OUT_MODE (0x0 << 9)
  57. #define DEFAULT_OP_MODE_CFG_VAL(cfg_x) (R_RDATA_FLAG_EN + R_PACK_START_EN +\
  58. R_RDOUT_MODE + R_FIFO_PREVENT_EN+\
  59. R_SAMP_OUT_MODE)
  60. #define SET_R_OP_MODE_A(val) ((val & 0xF000) >> 12)
  61. #define SET_R_DATAMODE_A(val) ((val & 0x0F00) >> 8)
  62. #define SET_R_OP_MODE_B(val) ((val & 0x00F0) >> 4)
  63. #define SET_R_DATAMODE_B(val) ((val & 0x000F))
  64. /**
  65. @brief Used to get the value need to be set for slot-A, B operating mode
  66. and data out mode.
  67. */
  68. #define SET_MODE_VALUE(val) (SET_R_OP_MODE_A(val) |\
  69. (SET_R_DATAMODE_A(val) << 2) |\
  70. (SET_R_OP_MODE_B(val) << 5) |\
  71. (SET_R_DATAMODE_B(val) << 6))
  72. /******************************************************************/
  73. /**************** ADPD142 INTERRUPT MASK *************************/
  74. #define INTR_MASK_IDLE_USR 0x0000
  75. #define INTR_MASK_SAMPLE_USR 0x0020
  76. #define INTR_MASK_S_SAMP_XY_A_USR 0x0020
  77. #define INTR_MASK_S_SAMP_XY_AB_USR 0x0040
  78. #define INTR_MASK_S_SAMP_XY_B_USR 0x0040
  79. #define SET_INTR_MASK(usr_val) ((~(INTR_MASK_##usr_val)) & 0x1FF)
  80. /******************************************************************/
  81. /***************** ADPD142 PRINT *********************************/
  82. #ifdef ADPD_DBG
  83. #define ADPD142_dbg(format, arg...) \
  84. printk(KERN_DEBUG "ADPD142 : "format, ##arg);
  85. #else
  86. #define ADPD142_dbg(format, arg...) {if (dbg_enable)\
  87. printk(KERN_DEBUG "ADPD142 : "format, ##arg);\
  88. }
  89. #endif
  90. #ifdef ADPD_INFO
  91. #define ADPD142_info(format, arg...) \
  92. printk(KERN_INFO "ADPD142 : "format, ##arg);
  93. #else
  94. #define ADPD142_info(format, arg...) {if (0)\
  95. ; }
  96. #endif
  97. /******************************************************************/
  98. /***************** ADPD142 DIFFERENT MODE ************************/
  99. /*
  100. Main mode
  101. */
  102. #define IDLE_USR 0
  103. #define SAMPLE_USR 3
  104. /*
  105. Sub mode
  106. */
  107. #define S_SAMP_XY_A 0
  108. #define S_SAMP_XY_AB 1
  109. #define S_SAMP_XY_B 2
  110. /*
  111. OPERATING MODE ==> MAIN_mode[20:16] |
  112. OP_mode_A[15:12] |
  113. DATA_mode_A[11:8]|
  114. OP_mode_B[7:4] |
  115. DATA_mode_B[3:0]
  116. */
  117. #define IDLE_OFF ((IDLE_USR << 16) | (0x0000))
  118. #define SAMPLE_XY_A ((SAMPLE_USR << 16) | (0x1400))
  119. #define SAMPLE_XY_AB ((SAMPLE_USR << 16) | (0x1414))
  120. #define SAMPLE_XY_B ((SAMPLE_USR << 16) | (0x0014))
  121. #define MAX_MODE 4
  122. #define MAX_IDLE_MODE 1
  123. #define MAX_SAMPLE_MODE 3
  124. #define MODE(val) __arr_mode_##val
  125. #define MODE_SIZE(val) MAX_##val##_MODE
  126. #define MAX_LIB_VER 20
  127. #define OSC_TRIM_ADDR26_REG 0x26
  128. #define OSC_TRIM_ADDR28_REG 0x28
  129. #define OSC_TRIM_ADDR29_REG 0x29
  130. #define OSC_TRIM_32K_REG 0x4B
  131. #define OSC_TRIM_32M_REG 0x4D
  132. #define OSC_DEFAULT_32K_SET 0x2695
  133. #define OSC_DEFAULT_32M_SET 0x4272
  134. #define OSCILLATOR_TRIM_FILE_PATH "/efs/osc_trim"
  135. /**
  136. mode mapping structure used to hold the number mode actually supported by
  137. driver
  138. */
  139. struct mode_mapping {
  140. char *name;
  141. int *mode_code;
  142. unsigned int size;
  143. };
  144. int __arr_mode_IDLE[MAX_IDLE_MODE] = { IDLE_OFF };
  145. int __arr_mode_SAMPLE[MAX_SAMPLE_MODE] = { SAMPLE_XY_A,
  146. SAMPLE_XY_AB,
  147. SAMPLE_XY_B };
  148. struct mode_mapping __mode_recv_frm_usr[MAX_MODE] = {
  149. {
  150. .name = "IDLE",
  151. .mode_code = MODE(IDLE),
  152. .size = MODE_SIZE(IDLE)
  153. },
  154. { .name = "UNSUPPORTED1",
  155. .mode_code = 0,
  156. .size = 0,
  157. },
  158. { .name = "UNSUPPORTED2",
  159. .mode_code = 0,
  160. .size = 0,
  161. },
  162. {
  163. .name = "SAMPLE",
  164. .mode_code = MODE(SAMPLE),
  165. .size = MODE_SIZE(SAMPLE)
  166. }
  167. };
  168. /**
  169. general data buffer that is required to store the recieved buffer
  170. */
  171. unsigned short data_buffer[MAX_BUFFER] = { 0 };
  172. struct wrk_q_timestamp {
  173. struct timeval interrupt_trigger;
  174. };
  175. struct adpd142_stats {
  176. atomic_t interrupts;
  177. atomic_t fifo_requires_sync;
  178. atomic_t fifo_bytes[4];
  179. atomic_t wq_pending;
  180. atomic_t wq_schedule_time_peak_usec;
  181. atomic_t wq_schedule_time_last_usec;
  182. struct ewma wq_schedule_time_avg_usec;
  183. atomic_t data_process_time_peak_usec;
  184. atomic_t data_process_time_last_usec;
  185. struct ewma data_process_time_avg_usec;
  186. struct wrk_q_timestamp stamp;
  187. };
  188. /**
  189. structure hold adpd142 chip structure, and parameter used
  190. to control the adpd142 software part.
  191. */
  192. struct adpd142_data {
  193. struct i2c_client *client;
  194. struct mutex mutex;/*for chip structure*/
  195. struct device *dev;
  196. struct input_dev *ptr_sample_inputdev;
  197. struct adpd_platform_data *ptr_config;
  198. struct work_struct work;
  199. struct workqueue_struct *ptr_adpd142_wq_st;
  200. int irq;
  201. int hrm_int;
  202. unsigned short *ptr_data_buffer;
  203. struct adpd142_stats stats;
  204. unsigned short intr_mask_val;
  205. unsigned short intr_status_val;
  206. unsigned short fifo_size;
  207. /*sysfs register read write */
  208. unsigned short sysfs_I2C_regaddr;
  209. unsigned short sysfs_I2C_regval;
  210. unsigned short sysfslastreadval;
  211. atomic_t sample_enabled;
  212. atomic_t adpd_mode;
  213. int (*read)(struct adpd142_data *, u8 reg_addr, int len, u16 *buf);
  214. int (*write)(struct adpd142_data *, u8 reg_addr, int len, u16 data);
  215. u8 eol_test_is_enable;
  216. u8 eol_test_status;
  217. char *eol_test_result;
  218. char *eol_lib_ver;
  219. char *elf_lib_ver;
  220. int osc_trim_32K_value;
  221. int osc_trim_32M_value;
  222. int osc_trim_addr26_value;
  223. int osc_trim_addr28_value;
  224. int osc_trim_addr29_value;
  225. u8 osc_trim_open_enable;
  226. struct regulator *reg_adpd;
  227. struct regulator *vdd_led;
  228. };
  229. /**
  230. structure hold adpd142 configuration data to be written during probe.
  231. */
  232. static struct adpd_platform_data adpd142_config_data = {
  233. .config_size = 53,
  234. .config_data = {
  235. 0x00100001,
  236. 0x000100ff,
  237. 0x00020004,
  238. 0x00060000,
  239. 0x00111011,
  240. 0x0012000A,
  241. 0x00130320,
  242. 0x00140449,
  243. 0x00150333,
  244. 0x001C4040,
  245. 0x001D4040,
  246. 0x00181400,
  247. 0x00191400,
  248. 0x001a1650,
  249. 0x001b1000,
  250. 0x001e1ff0,
  251. 0x001f1Fc0,
  252. 0x00201ad0,
  253. 0x00211470,
  254. 0x00233032,
  255. 0x00243035,
  256. 0x002502CC,
  257. 0x00340000,
  258. 0x00260000,
  259. 0x00270800,
  260. 0x00280000,
  261. 0x00294003,
  262. 0x00300330,
  263. 0x00310213,
  264. 0x00421d37,
  265. 0x0043ada5,
  266. 0x003924D2,
  267. 0x00350330,
  268. 0x00360813,
  269. 0x00441d37,
  270. 0x0045ada5,
  271. 0x003b24D2,
  272. 0x00590808,
  273. 0x00320320,
  274. 0x00330113,
  275. 0x003a22d4,
  276. 0x003c0006,
  277. 0x00401010,
  278. 0x0041004c,
  279. 0x004c0004,
  280. 0x004f2090,
  281. 0x00500000,
  282. 0x00523000,
  283. 0x0053e400,
  284. 0x005b1831,
  285. 0x005d1b31,
  286. 0x005e2808,
  287. 0x004e0040,
  288. },
  289. };
  290. /**
  291. This function is used to parse data from the string "0xXXX 1 0xYYY"
  292. @param buf is pointer point constant address
  293. @param cnt store number value need to parse
  294. @param data parse data are stored in it
  295. @return void
  296. */
  297. static void
  298. cmd_parsing(const char *buf, unsigned short cnt, unsigned short *data)
  299. {
  300. char **bp = (char **)&buf;
  301. char *token, minus, parsing_cnt = 0;
  302. int val;
  303. int pos;
  304. while ((token = strsep(bp, " "))) {
  305. pos = 0;
  306. minus = false;
  307. if ((char)token[pos] == '-') {
  308. minus = true;
  309. pos++;
  310. }
  311. if ((token[pos] == '0') &&
  312. (token[pos + 1] == 'x' || token[pos + 1] == 'X')) {
  313. if (kstrtoul(&token[pos + 2], 16,
  314. (long unsigned int *)&val))
  315. val = 0;
  316. if (minus)
  317. val *= (-1);
  318. } else {
  319. if (kstrtoul(&token[pos], 10,
  320. (long unsigned int *)&val))
  321. val = 0;
  322. if (minus)
  323. val *= (-1);
  324. }
  325. if (parsing_cnt < cnt)
  326. *(data + parsing_cnt) = (unsigned short)val;
  327. else
  328. break;
  329. parsing_cnt++;
  330. }
  331. }
  332. /**
  333. This function is a inline function mapped to i2c read functionality
  334. @param pst_adpd the ADPD142 data structure
  335. @param reg is the address from which the value need to be read out
  336. @return u16 value present in the register.
  337. */
  338. static inline u16
  339. reg_read(struct adpd142_data *pst_adpd, u16 reg)
  340. {
  341. u16 value;
  342. if (!pst_adpd->read(pst_adpd, reg, 1, &value))
  343. return value;
  344. return 0xFFFF;
  345. }
  346. /**
  347. This function is a inline function mapped to i2c read functionality
  348. @param pst_adpd the ADPD142 data structure
  349. @param reg is the address from which the value need to be read out
  350. @param len number of data need to be readout
  351. @param buf is pointer store the value present in the register.
  352. @return u16 status of i2c read.
  353. */
  354. static inline u16
  355. multi_reg_read(struct adpd142_data *pst_adpd, u16 reg, u16 len, u16 *buf)
  356. {
  357. return pst_adpd->read(pst_adpd, reg, len, buf);
  358. }
  359. /**
  360. This function is a inline function mapped to i2c write functionality
  361. @param pst_adpd the ADPD142 data structure
  362. @param reg is the address to which the data need to be written
  363. @param value is the data need to be write.
  364. @return u16 status of i2c write.
  365. */
  366. static inline u16
  367. reg_write(struct adpd142_data *pst_adpd, u16 reg, u16 value)
  368. {
  369. return pst_adpd->write(pst_adpd, reg, 1, value);
  370. }
  371. /**
  372. This function is used for I2C read from the sysfs
  373. file system of the ADPD142 Chip
  374. @param pst_adpd the ADPD142 data structure
  375. @return u16 status of i2c read.
  376. */
  377. static u16
  378. adpd142_sysfs_I2C_read(struct adpd142_data *pst_adpd)
  379. {
  380. return reg_read(pst_adpd, pst_adpd->sysfs_I2C_regaddr);
  381. }
  382. /**
  383. This function is used for I2C write from the sysfs
  384. file system of the ADPD142 Chip
  385. @param pst_adpd the ADPD142 data structure
  386. @return u16 the bytes of written data.
  387. */
  388. static u16
  389. adpd142_sysfs_I2C_write(struct adpd142_data *pst_adpd)
  390. {
  391. u16 err;
  392. err = reg_write(pst_adpd, pst_adpd->sysfs_I2C_regaddr,
  393. pst_adpd->sysfs_I2C_regval);
  394. if (err)
  395. return 0xFFFF;
  396. else
  397. return pst_adpd->sysfs_I2C_regval;
  398. }
  399. /**
  400. This function is used to parse string
  401. @param recv_buf pointer point a string
  402. @return int
  403. */
  404. static int
  405. parse_data(char *recv_buf)
  406. {
  407. char **bp = (char **)&recv_buf;
  408. char *token, parsing_cnt = 0;
  409. long val;
  410. int test_data;
  411. unsigned int data = 0;
  412. int ret = 0;
  413. char test[10] = {'\0'};
  414. while ((token = strsep(bp, " \t"))) {
  415. memset(test, '\0', 10);
  416. memcpy(test, token, strlen(token));
  417. memmove(test+2, test, strlen(test));
  418. test[0] = '0';
  419. test[1] = 'x';
  420. ret = kstrtol(test, 0, &val);
  421. if (ret) {
  422. if (ret == -ERANGE) {
  423. ADPD142_info("out of range\n");
  424. val = 0;
  425. }
  426. if (ret == -EINVAL) {
  427. ADPD142_info("parsing error\n");
  428. sscanf(test, "%x", &test_data);
  429. val = test_data;
  430. }
  431. }
  432. if (parsing_cnt == 0) {
  433. data = (int) (0xFF & val);
  434. if (data == 0)
  435. return -1;
  436. }
  437. if (parsing_cnt == 1) {
  438. data = data << 16;
  439. data |= (0xFFFF & val);
  440. }
  441. parsing_cnt++;
  442. if (parsing_cnt > 2)
  443. break;
  444. }
  445. return data;
  446. }
  447. /**
  448. This function is used for reading configuration file
  449. @param pst_adpd the ADPD142 data structure
  450. @return int status of the configuration file.
  451. */
  452. static int
  453. adpd142_read_config_file(struct adpd142_data *pst_adpd)
  454. {
  455. mm_segment_t old_fs;
  456. struct file *fpt_adpd = NULL;
  457. struct adpd_platform_data *ptr_config = NULL;
  458. int line_cnt = 0;
  459. int start = 0;
  460. int cnt = 0;
  461. int ret = 0;
  462. int i = 0;
  463. char get_char;
  464. char *recieved_buf = NULL;
  465. loff_t pos = 0;
  466. ptr_config = pst_adpd->ptr_config;
  467. old_fs = get_fs();
  468. set_fs(KERNEL_DS);
  469. fpt_adpd = filp_open("/data/misc/adpd142_config.dcfg", O_RDONLY, 0);
  470. if (IS_ERR(fpt_adpd)) {
  471. ADPD142_dbg("unable to find de file %ld\n", PTR_ERR(fpt_adpd));
  472. set_fs(old_fs);
  473. ret = /*PTR_ERR(fpt_adpd_filp); */ -1;
  474. ptr_config->config_size = 0;
  475. goto err_filp_open;
  476. }
  477. recieved_buf = kzalloc(sizeof(char) * 15, GFP_KERNEL);
  478. memset(recieved_buf, '\0', sizeof(recieved_buf));
  479. while (vfs_read(fpt_adpd, &get_char, sizeof(char), &pos)) {
  480. if (get_char == '\n') {
  481. line_cnt++;
  482. if (cnt > 1) {
  483. ret = parse_data(recieved_buf);
  484. if (ret == -1) {
  485. ADPD142_dbg("Error in reading dcfg\n");
  486. break;
  487. }
  488. ADPD142_info("0x%08x\n", ret);
  489. ptr_config->config_data[i] = ret;
  490. i++;
  491. }
  492. memset(recieved_buf, '\0', sizeof(recieved_buf));
  493. start = 0;
  494. cnt = 0;
  495. ret = 0;
  496. } else {
  497. if (get_char != '#') {
  498. if (start != 0xF) {
  499. recieved_buf[cnt] = get_char;
  500. cnt++;
  501. }
  502. } else {
  503. start = 0xF;
  504. }
  505. }
  506. }
  507. filp_close(fpt_adpd, NULL);
  508. set_fs(old_fs);
  509. kfree(recieved_buf);
  510. ptr_config->config_size = i;
  511. return 0;
  512. err_filp_open:
  513. return -1;
  514. }
  515. /**
  516. This function is used for reading interrupt and FIFO status
  517. @param pst_adpd the ADPD142 data structure
  518. @return unsigned short interrupt status value
  519. */
  520. static unsigned short
  521. adpd142_rd_intr_fifo(struct adpd142_data *pst_adpd)
  522. {
  523. unsigned short regval = 0;
  524. unsigned short fifo_size = 0;
  525. unsigned short usr_mode = 0;
  526. regval = reg_read(pst_adpd, ADPD_INT_STATUS_ADDR);
  527. fifo_size = ((regval & 0xFF00) >> 8);
  528. pst_adpd->fifo_size = ((fifo_size / 2) + (fifo_size & 1));
  529. pst_adpd->intr_status_val = (regval & 0xFF);
  530. ADPD142_info("Intr_status 0x%x, FIFO_SIZE 0x%x\n",
  531. pst_adpd->intr_status_val, pst_adpd->fifo_size);
  532. if (fifo_size <= 16)
  533. atomic_inc(&pst_adpd->stats.fifo_bytes[0]);
  534. else if (fifo_size <= 32)
  535. atomic_inc(&pst_adpd->stats.fifo_bytes[1]);
  536. else if (fifo_size <= 64)
  537. atomic_inc(&pst_adpd->stats.fifo_bytes[2]);
  538. else
  539. atomic_inc(&pst_adpd->stats.fifo_bytes[3]);
  540. usr_mode = atomic_read(&pst_adpd->adpd_mode);
  541. if (0 != usr_mode) {
  542. unsigned int mask = 0;
  543. unsigned int mod = 0;
  544. switch (GET_USR_SUB_MODE(usr_mode)) {
  545. case S_SAMP_XY_AB:
  546. mask = 0x07;
  547. break;
  548. default:
  549. mask = 0x03;
  550. break;
  551. };
  552. mod = pst_adpd->fifo_size & mask;
  553. if (mod) {
  554. ADPD142_info("Keeping %d samples in FIFO from %d\n",
  555. mod, pst_adpd->fifo_size);
  556. pst_adpd->fifo_size &= ~mask;
  557. atomic_inc(&pst_adpd->stats.fifo_requires_sync);
  558. }
  559. }
  560. return pst_adpd->intr_status_val;
  561. }
  562. /**
  563. This function is used for clearing the Interrupt as well as FIFO
  564. @param pst_adpd the ADPD142 data structure
  565. @return void
  566. */
  567. static void
  568. adpd142_clr_intr_fifo(struct adpd142_data *pst_adpd)
  569. {
  570. /*below code is added for verification */
  571. unsigned short regval;
  572. regval = reg_read(pst_adpd, ADPD_INT_STATUS_ADDR);
  573. ADPD142_info("fifo_size: 0x%04x, Status: 0x%x\n",
  574. ((regval & 0xFF00) >> 8), (regval & 0xFF));
  575. reg_write(pst_adpd, ADPD_INT_STATUS_ADDR, 0x80FF);
  576. regval = reg_read(pst_adpd, ADPD_INT_STATUS_ADDR);
  577. ADPD142_info("After clear - fifo_size: 0x%04x, Status: 0x%x\n",
  578. ((regval & 0xFF00) >> 8), (regval & 0xFF));
  579. }
  580. /**
  581. This function is used for clearing the Interrupt status
  582. @param pst_adpd the ADPD142 data structure
  583. @param mode operating mode of ADPD142
  584. @return void
  585. */
  586. static void
  587. adpd142_clr_intr_status(struct adpd142_data *pst_adpd, unsigned short mode)
  588. {
  589. reg_write(pst_adpd, ADPD_INT_STATUS_ADDR,
  590. pst_adpd->intr_status_val);
  591. }
  592. /**
  593. This function is used to read out FIFO data from FIFO
  594. @param pst_adpd the ADPD142 data structure
  595. @return unsigned int FIFO size
  596. */
  597. static unsigned int
  598. adpd142_rd_fifo_data(struct adpd142_data *pst_adpd)
  599. {
  600. int loop_cnt = 0;
  601. if (!pst_adpd->fifo_size)
  602. goto err_rd_fifo_data;
  603. if (0 != atomic_read(&pst_adpd->adpd_mode) &&
  604. pst_adpd->fifo_size & 0x3) {
  605. pr_err("Unexpected FIFO_SIZE=%d,\
  606. should be multiple of four (channels)!\n",
  607. pst_adpd->fifo_size);
  608. }
  609. /* reg_write(pst_adpd, ADPD_ACCESS_CTRL_ADDR, 0x0001);*/
  610. reg_write(pst_adpd, ADPD_TEST_PD_ADDR, EN_FIFO_CLK);
  611. multi_reg_read(pst_adpd, ADPD_DATA_BUFFER_ADDR,
  612. pst_adpd->fifo_size, pst_adpd->ptr_data_buffer);
  613. /* reg_write(pst_adpd, ADPD_ACCESS_CTRL_ADDR, 0x0000);*/
  614. reg_write(pst_adpd, ADPD_TEST_PD_ADDR, DS_FIFO_CLK);
  615. for (; loop_cnt < pst_adpd->fifo_size; loop_cnt++) {
  616. ADPD142_info("[0x%04x] 0x%04x\n", loop_cnt,
  617. pst_adpd->ptr_data_buffer[loop_cnt]);
  618. }
  619. return pst_adpd->fifo_size;
  620. err_rd_fifo_data:
  621. return 0;
  622. }
  623. /**
  624. This function is a thing need to configure before changing the register
  625. @param pst_adpd the ADPD142 data structure
  626. @param global_mode OFF, IDLE, GO are the three mode
  627. @return void
  628. */
  629. static void
  630. adpd142_config_prerequisite(struct adpd142_data *pst_adpd, u32 global_mode)
  631. {
  632. unsigned short regval = 0;
  633. regval = reg_read(pst_adpd, ADPD_OP_MODE_ADDR);
  634. regval = SET_GLOBAL_OP_MODE(regval, global_mode);
  635. ADPD142_info("reg 0x%04x,\tafter set 0x%04x\n",
  636. regval, SET_GLOBAL_OP_MODE(regval, global_mode));
  637. reg_write(pst_adpd, ADPD_OP_MODE_ADDR, regval);
  638. }
  639. /**
  640. This function is used for switching the adpd142 mode
  641. @param pst_adpd the ADPD142 data structure
  642. @param usr_mode user mode
  643. @return void
  644. */
  645. static void
  646. adpd142_mode_switching(struct adpd142_data *pst_adpd, u32 usr_mode)
  647. {
  648. unsigned int opmode_val = 0;
  649. unsigned short mode_val = 0;
  650. unsigned short intr_mask_val = 0;
  651. unsigned short mode = GET_USR_MODE(usr_mode);
  652. unsigned short sub_mode = GET_USR_SUB_MODE(usr_mode);
  653. /*disabling further avoid further interrupt to trigger */
  654. disable_irq(pst_adpd->irq);
  655. /*stop the pending work */
  656. /*this function Gurantee that wrk is not pending or executing on CPU */
  657. cancel_work_sync(&pst_adpd->work);
  658. atomic_set(&pst_adpd->adpd_mode, 0);
  659. mutex_lock(&pst_adpd->mutex);
  660. /*Depending upon the mode get the value need to write Operatin mode*/
  661. opmode_val = *(__mode_recv_frm_usr[mode].mode_code + (sub_mode));
  662. adpd142_config_prerequisite(pst_adpd, GLOBAL_OP_MODE_IDLE);
  663. /*switch to IDLE mode */
  664. mode_val = DEFAULT_OP_MODE_CFG_VAL(pst_adpd);
  665. mode_val += SET_MODE_VALUE(IDLE_OFF);
  666. intr_mask_val = SET_INTR_MASK(IDLE_USR);
  667. reg_write(pst_adpd, ADPD_OP_MODE_CFG_ADDR, mode_val);
  668. reg_write(pst_adpd, ADPD_INT_MASK_ADDR, intr_mask_val);
  669. /*clear FIFO and flush buffer */
  670. adpd142_clr_intr_fifo(pst_adpd);
  671. adpd142_rd_intr_fifo(pst_adpd);
  672. if (pst_adpd->fifo_size != 0) {
  673. adpd142_clr_intr_status(pst_adpd, IDLE_USR);
  674. ADPD142_info("Flushing FIFO\n");
  675. adpd142_rd_fifo_data(pst_adpd);
  676. }
  677. adpd142_config_prerequisite(pst_adpd, GLOBAL_OP_MODE_GO);
  678. msleep(20);
  679. adpd142_config_prerequisite(pst_adpd, GLOBAL_OP_MODE_IDLE);
  680. /*Find Interrupt mask value */
  681. switch (mode) {
  682. case IDLE_USR:
  683. ADPD142_info("IDLE MODE\n");
  684. intr_mask_val = SET_INTR_MASK(IDLE_USR);
  685. atomic_set(&pst_adpd->sample_enabled, 0);
  686. break;
  687. case SAMPLE_USR:
  688. ADPD142_info("SAMPLE MODE\n");
  689. /*enable interrupt only when data written to FIFO */
  690. switch (sub_mode) {
  691. case S_SAMP_XY_A:
  692. intr_mask_val = SET_INTR_MASK(S_SAMP_XY_A_USR);
  693. break;
  694. case S_SAMP_XY_AB:
  695. intr_mask_val = SET_INTR_MASK(S_SAMP_XY_AB_USR);
  696. break;
  697. case S_SAMP_XY_B:
  698. intr_mask_val = SET_INTR_MASK(S_SAMP_XY_B_USR);
  699. break;
  700. default:
  701. intr_mask_val = SET_INTR_MASK(SAMPLE_USR);
  702. break;
  703. };
  704. intr_mask_val |= 0xC000;
  705. atomic_set(&pst_adpd->sample_enabled, 1);
  706. break;
  707. default:
  708. /*This case won't occur */
  709. ADPD142_dbg("invalid mode\n");
  710. break;
  711. };
  712. ADPD142_info("INT_MASK_ADDR[0x%04x] = 0x%04x\n", ADPD_INT_MASK_ADDR,
  713. intr_mask_val);
  714. pst_adpd->intr_mask_val = intr_mask_val;
  715. /*Fetch Default opmode configuration other than OP mode
  716. and DATA_OUT mode */
  717. mode_val = DEFAULT_OP_MODE_CFG_VAL(pst_adpd);
  718. /*Get Mode value from the opmode value,
  719. to hardocde the macro, change SET_MODE_VALUE macro and concern
  720. mode macro
  721. */
  722. mode_val += SET_MODE_VALUE(opmode_val);
  723. ADPD142_info("OP_MODE_CFG[0x%04x] = 0x%04x\n",
  724. ADPD_OP_MODE_CFG_ADDR, mode_val);
  725. atomic_set(&pst_adpd->adpd_mode, usr_mode);
  726. reg_write(pst_adpd, ADPD_INT_MASK_ADDR, pst_adpd->intr_mask_val);
  727. reg_write(pst_adpd, ADPD_OP_MODE_CFG_ADDR, mode_val);
  728. mutex_unlock(&pst_adpd->mutex);
  729. enable_irq(pst_adpd->irq);
  730. if (mode != IDLE_USR)
  731. adpd142_config_prerequisite(pst_adpd, GLOBAL_OP_MODE_GO);
  732. else
  733. adpd142_config_prerequisite(pst_adpd, GLOBAL_OP_MODE_OFF);
  734. }
  735. /**
  736. This function is used for sending Sample event depend upon
  737. the Sample mode
  738. @param pst_adpd the ADPD142 data structure
  739. @return void
  740. */
  741. static void
  742. adpd142_sample_event(struct adpd142_data *pst_adpd)
  743. {
  744. unsigned short usr_mode = 0;
  745. unsigned short sub_mode = 0;
  746. unsigned short cnt = 0;
  747. ADPD142_info("%s\n", __func__);
  748. usr_mode = atomic_read(&pst_adpd->adpd_mode);
  749. sub_mode = GET_USR_SUB_MODE(usr_mode);
  750. switch (sub_mode) {
  751. case S_SAMP_XY_A:
  752. if (pst_adpd->fifo_size < 4 || (pst_adpd->fifo_size & 0x3)) {
  753. pr_err("Unexpected FIFO_SIZE=%d\n",
  754. pst_adpd->fifo_size);
  755. break;
  756. }
  757. for (cnt = 0; cnt < pst_adpd->fifo_size; cnt += 4) {
  758. input_event(pst_adpd->ptr_sample_inputdev, EV_REL,
  759. REL_Z, sub_mode + 1);
  760. input_event(pst_adpd->ptr_sample_inputdev, EV_MSC,
  761. MSC_RAW, pst_adpd->ptr_data_buffer[cnt] + 1);
  762. input_event(pst_adpd->ptr_sample_inputdev, EV_MSC,
  763. MSC_RAW, pst_adpd->ptr_data_buffer[cnt + 1] + 1);
  764. input_event(pst_adpd->ptr_sample_inputdev, EV_MSC,
  765. MSC_RAW, pst_adpd->ptr_data_buffer[cnt + 2] + 1);
  766. input_event(pst_adpd->ptr_sample_inputdev, EV_MSC,
  767. MSC_RAW, pst_adpd->ptr_data_buffer[cnt + 3] + 1);
  768. input_sync(pst_adpd->ptr_sample_inputdev);
  769. }
  770. break;
  771. case S_SAMP_XY_AB:
  772. if (pst_adpd->fifo_size < 8 || (pst_adpd->fifo_size & 0x7)) {
  773. pr_err("Unexpected FIFO_SIZE=%d\n",
  774. pst_adpd->fifo_size);
  775. break;
  776. }
  777. for (cnt = 0; cnt < pst_adpd->fifo_size; cnt += 8) {
  778. unsigned int channel;
  779. unsigned int sum_slot_a = 0;
  780. unsigned int sum_slot_b = 0;
  781. input_event(pst_adpd->ptr_sample_inputdev,
  782. EV_REL, REL_Z, sub_mode + 1);
  783. for (channel = 0; channel < 4; ++channel) {
  784. sum_slot_a +=
  785. pst_adpd->ptr_data_buffer[cnt+channel];
  786. input_event(pst_adpd->ptr_sample_inputdev,
  787. EV_MSC, MSC_RAW, pst_adpd->ptr_data_buffer[cnt+channel] + 1);
  788. }
  789. input_event(pst_adpd->ptr_sample_inputdev,
  790. EV_REL, REL_X, sum_slot_a + 1);
  791. for (channel = 4; channel < 8; ++channel) {
  792. sum_slot_b +=
  793. pst_adpd->ptr_data_buffer[cnt+channel];
  794. input_event(pst_adpd->ptr_sample_inputdev,
  795. EV_MSC, MSC_RAW, pst_adpd->ptr_data_buffer[cnt+channel] + 1);
  796. }
  797. input_event(pst_adpd->ptr_sample_inputdev,
  798. EV_REL, REL_Y, sum_slot_b + 1);
  799. input_sync(pst_adpd->ptr_sample_inputdev);
  800. }
  801. break;
  802. case S_SAMP_XY_B:
  803. if (pst_adpd->fifo_size < 4 || (pst_adpd->fifo_size & 0x3)) {
  804. pr_err("Unexpected FIFO_SIZE=%d\n",
  805. pst_adpd->fifo_size);
  806. break;
  807. }
  808. for (cnt = 0; cnt < pst_adpd->fifo_size; cnt += 4) {
  809. input_event(pst_adpd->ptr_sample_inputdev, EV_REL,
  810. REL_Z,
  811. sub_mode + 1);
  812. input_event(pst_adpd->ptr_sample_inputdev, EV_MSC,
  813. MSC_RAW,
  814. pst_adpd->ptr_data_buffer[cnt] + 1);
  815. input_event(pst_adpd->ptr_sample_inputdev, EV_MSC,
  816. MSC_RAW,
  817. pst_adpd->ptr_data_buffer[cnt + 1] + 1);
  818. input_event(pst_adpd->ptr_sample_inputdev, EV_MSC,
  819. MSC_RAW,
  820. pst_adpd->ptr_data_buffer[cnt + 2] + 1);
  821. input_event(pst_adpd->ptr_sample_inputdev, EV_MSC,
  822. MSC_RAW,
  823. pst_adpd->ptr_data_buffer[cnt + 3] + 1);
  824. input_sync(pst_adpd->ptr_sample_inputdev);
  825. }
  826. break;
  827. default:
  828. break;
  829. };
  830. }
  831. /**
  832. This function is used for handling FIFO when interrupt occured
  833. @param pst_adpd the ADPD142 data structure
  834. @return void
  835. */
  836. static void
  837. adpd142_data_handler(struct adpd142_data *pst_adpd)
  838. {
  839. unsigned short usr_mode = 0;
  840. unsigned short mode = 0;
  841. unsigned short sub_mode = 0;
  842. mutex_lock(&pst_adpd->mutex);
  843. usr_mode = atomic_read(&pst_adpd->adpd_mode);
  844. mode = GET_USR_MODE(usr_mode);
  845. sub_mode = GET_USR_SUB_MODE(usr_mode);
  846. ADPD142_info("mode - 0x%x,\t sub_mode - 0x%x\n", mode, sub_mode);
  847. adpd142_rd_intr_fifo(pst_adpd);
  848. if (pst_adpd->intr_status_val == 0) {
  849. mutex_unlock(&pst_adpd->mutex);
  850. return;
  851. }
  852. adpd142_clr_intr_status(pst_adpd, mode);
  853. switch (mode) {
  854. case IDLE_USR:
  855. ADPD142_dbg("IDLE_MODE\n");
  856. adpd142_rd_fifo_data(pst_adpd);
  857. break;
  858. case SAMPLE_USR:
  859. ADPD142_info("SAMPLE MODE\n");
  860. adpd142_rd_fifo_data(pst_adpd);
  861. adpd142_sample_event(pst_adpd);
  862. break;
  863. default:
  864. ADPD142_info("DEFAULT MODE\n");
  865. adpd142_rd_fifo_data(pst_adpd);
  866. break;
  867. };
  868. mutex_unlock(&pst_adpd->mutex);
  869. }
  870. /**
  871. This function is a handler for WorkQueue
  872. @param ptr_work linux work structure
  873. @return void
  874. */
  875. static void
  876. adpd142_wq_handler(struct work_struct *ptr_work)
  877. {
  878. struct adpd142_data *pst_adpd = container_of(ptr_work,
  879. struct adpd142_data, work);
  880. struct timeval wkq_start;
  881. struct timeval wkq_comp;
  882. int diff_usec = 0;
  883. do_gettimeofday(&wkq_start);
  884. diff_usec = timeval_compare(&wkq_start,
  885. &pst_adpd->stats.stamp.interrupt_trigger);
  886. if (diff_usec > 1) {
  887. if (diff_usec >
  888. atomic_read(
  889. &pst_adpd->stats.wq_schedule_time_peak_usec))
  890. atomic_set(
  891. &pst_adpd->stats.wq_schedule_time_peak_usec,
  892. diff_usec);
  893. atomic_set(&pst_adpd->stats.wq_schedule_time_last_usec,
  894. diff_usec);
  895. ewma_add(&pst_adpd->stats.wq_schedule_time_avg_usec, diff_usec);
  896. }
  897. adpd142_data_handler(pst_adpd);
  898. do_gettimeofday(&wkq_comp);
  899. diff_usec = timeval_compare(&wkq_comp, &wkq_start);
  900. if (diff_usec > 1) {
  901. if (diff_usec >
  902. atomic_read(&pst_adpd->stats.data_process_time_peak_usec))
  903. atomic_set(
  904. &pst_adpd->stats.data_process_time_peak_usec,
  905. diff_usec);
  906. atomic_set(&pst_adpd->stats.data_process_time_last_usec,
  907. diff_usec);
  908. ewma_add(&pst_adpd->stats.data_process_time_avg_usec,
  909. diff_usec);
  910. }
  911. }
  912. /**
  913. This function is used for handling Interrupt from ADPD142
  914. @param irq is Interrupt number
  915. @param dev_id is pointer point to ADPD142 data structure
  916. @return irqreturn_t is a Interrupt flag
  917. */
  918. static irqreturn_t
  919. adpd142_isr_handler(int irq, void *dev_id)
  920. {
  921. struct adpd142_data *pst_adpd = dev_id;
  922. atomic_inc(&pst_adpd->stats.interrupts);
  923. if (!work_pending(&pst_adpd->work)) {
  924. do_gettimeofday(&pst_adpd->stats.stamp.interrupt_trigger);
  925. ADPD142_info("%s\n", __func__);
  926. if (!queue_work(pst_adpd->ptr_adpd142_wq_st, &pst_adpd->work))
  927. atomic_inc(&pst_adpd->stats.wq_pending);
  928. } else {
  929. atomic_inc(&pst_adpd->stats.wq_pending);
  930. ADPD142_info("work_pending !!\n");
  931. }
  932. return IRQ_HANDLED;
  933. }
  934. /**
  935. This function is used for updating the ADPD142 structure after configuration
  936. @param pst_adpd the ADPD142 data structure
  937. @return void
  938. */
  939. static void
  940. adpd142_update_config(struct adpd142_data *pst_adpd)
  941. {
  942. return;
  943. }
  944. /**
  945. This function is used for loading the configuration data to ADPD142 chip
  946. 0 - From file "/data/misc/adpd142_configuration.dcfg"
  947. 1 - From Static defined Array
  948. @param pst_adpd the ADPD142 data structure
  949. @param config configuration command
  950. @return int status
  951. */
  952. static int
  953. adpd142_configuration(struct adpd142_data *pst_adpd, unsigned char config)
  954. {
  955. struct adpd_platform_data *ptr_config;
  956. unsigned short addr;
  957. unsigned short data;
  958. unsigned short cnt = 0;
  959. int ret = 0;
  960. adpd142_mode_switching(pst_adpd, 0);
  961. if (config == FRM_FILE) {
  962. ret = adpd142_read_config_file(pst_adpd);
  963. ADPD142_info("ARRAY_SIZE - %d\n", size);
  964. } else {
  965. ret = FRM_ARR;
  966. }
  967. if (ret == 0)
  968. ptr_config = pst_adpd->ptr_config;
  969. else
  970. ptr_config = &adpd142_config_data;
  971. for (cnt = 0; cnt < ptr_config->config_size; cnt++) {
  972. addr = (unsigned short) ((0xFFFF0000 &
  973. ptr_config->config_data[cnt]) >> 16);
  974. data = (unsigned short) (0x0000FFFF &
  975. ptr_config->config_data[cnt]);
  976. ADPD142_dbg("addr[0x%04x] = 0x%04x\n", addr, data);
  977. reg_write(pst_adpd, addr, data);
  978. }
  979. adpd142_update_config(pst_adpd);
  980. return 0;
  981. }
  982. /* read & write efs for hrm sensor */
  983. static int osc_trim_efs_register_open(struct adpd142_data *pst_adpd)
  984. {
  985. struct file *osc_filp = NULL;
  986. char buffer[60] = {0, };
  987. int err = 0;
  988. int osc_trim_32k, osc_trim_32m, osc_trim_addr26, osc_trim_addr28, osc_trim_addr29;
  989. mm_segment_t old_fs;
  990. old_fs = get_fs();
  991. set_fs(KERNEL_DS);
  992. osc_filp = filp_open(OSCILLATOR_TRIM_FILE_PATH, O_RDONLY, 0);
  993. if (IS_ERR(osc_filp)) {
  994. err = PTR_ERR(osc_filp);
  995. if (err != -ENOENT)
  996. pr_err("adpd142_%s: Can't open oscillator trim file\n", __func__);
  997. set_fs(old_fs);
  998. return err;
  999. }
  1000. err = osc_filp->f_op->read(osc_filp,
  1001. (char *)buffer,
  1002. 60 * sizeof(char), &osc_filp->f_pos);
  1003. if (err != (60 * sizeof(char))) {
  1004. pr_err("adpd142_%s: Can't read the oscillator trim data from file\n", __func__);
  1005. err = -EIO;
  1006. }
  1007. sscanf(buffer, "%d,%d,%d,%d,%d",
  1008. &osc_trim_32k, &osc_trim_32m, &osc_trim_addr26, &osc_trim_addr28, &osc_trim_addr29);
  1009. pr_info("adpd142_%s = (%d,%d,%d,%d,%d)\n",
  1010. __func__, osc_trim_32k, osc_trim_32m, osc_trim_addr26, osc_trim_addr28, osc_trim_addr29);
  1011. pst_adpd->osc_trim_32K_value = osc_trim_32k;
  1012. pst_adpd->osc_trim_32M_value = osc_trim_32m;
  1013. pst_adpd->osc_trim_addr26_value = osc_trim_addr26;
  1014. pst_adpd->osc_trim_addr28_value = osc_trim_addr28;
  1015. pst_adpd->osc_trim_addr29_value = osc_trim_addr29;
  1016. filp_close(osc_filp, current->files);
  1017. set_fs(old_fs);
  1018. return err;
  1019. }
  1020. static int osc_trim_efs_register_save(struct adpd142_data *pst_adpd)
  1021. {
  1022. struct file *osc_filp = NULL;
  1023. mm_segment_t old_fs;
  1024. char buffer[60] = {0, };
  1025. int osc_trim_32k, osc_trim_32m, osc_trim_addr26, osc_trim_addr28, osc_trim_addr29;
  1026. int err = 0;
  1027. old_fs = get_fs();
  1028. set_fs(KERNEL_DS);
  1029. osc_filp = filp_open(OSCILLATOR_TRIM_FILE_PATH,
  1030. O_CREAT | O_TRUNC | O_WRONLY, 0660);
  1031. if (IS_ERR(osc_filp)) {
  1032. pr_err("adpd142_%s: Can't open oscillator trim file\n", __func__);
  1033. set_fs(old_fs);
  1034. err = PTR_ERR(osc_filp);
  1035. return err;
  1036. }
  1037. osc_trim_32k = reg_read(pst_adpd, OSC_TRIM_32K_REG);
  1038. osc_trim_32m = reg_read(pst_adpd, OSC_TRIM_32M_REG);
  1039. osc_trim_addr26 = reg_read(pst_adpd, OSC_TRIM_ADDR26_REG);
  1040. osc_trim_addr28 = reg_read(pst_adpd, OSC_TRIM_ADDR28_REG);
  1041. osc_trim_addr29 = reg_read(pst_adpd, OSC_TRIM_ADDR29_REG);
  1042. sprintf(buffer, "%d,%d,%d,%d,%d",
  1043. osc_trim_32k, osc_trim_32m, osc_trim_addr26, osc_trim_addr28, osc_trim_addr29);
  1044. pr_info("adpd142_%s = (%d,%d,%d,%d,%d)\n",
  1045. __func__, osc_trim_32k, osc_trim_32m, osc_trim_addr26, osc_trim_addr28, osc_trim_addr29);
  1046. err = osc_filp->f_op->write(osc_filp,
  1047. (char *)&buffer,
  1048. 60 * sizeof(char), &osc_filp->f_pos);
  1049. if (err != (60 * sizeof(char))) {
  1050. pr_err("adpd142_%s: Can't write the oscillator trim data to file\n", __func__);
  1051. err = -EIO;
  1052. }
  1053. pst_adpd->osc_trim_32K_value = osc_trim_32k;
  1054. pst_adpd->osc_trim_32M_value = osc_trim_32m;
  1055. pst_adpd->osc_trim_addr26_value = osc_trim_addr26;
  1056. pst_adpd->osc_trim_addr28_value = osc_trim_addr28;
  1057. pst_adpd->osc_trim_addr29_value = osc_trim_addr29;
  1058. filp_close(osc_filp, current->files);
  1059. set_fs(old_fs);
  1060. return err;
  1061. }
  1062. /**
  1063. This function clears all the statistic counters.
  1064. @param pst_adpd the ADPD142 data structure
  1065. @return void
  1066. */
  1067. static void
  1068. adpd_stat_reset(struct adpd142_data *pst_adpd)
  1069. {
  1070. atomic_set(&pst_adpd->stats.interrupts, 0);
  1071. atomic_set(&pst_adpd->stats.wq_pending, 0);
  1072. atomic_set(&pst_adpd->stats.wq_schedule_time_peak_usec, 0);
  1073. atomic_set(&pst_adpd->stats.wq_schedule_time_last_usec, 0);
  1074. atomic_set(&pst_adpd->stats.data_process_time_peak_usec, 0);
  1075. atomic_set(&pst_adpd->stats.data_process_time_last_usec, 0);
  1076. atomic_set(&pst_adpd->stats.fifo_requires_sync, 0);
  1077. atomic_set(&pst_adpd->stats.fifo_bytes[0], 0);
  1078. atomic_set(&pst_adpd->stats.fifo_bytes[1], 0);
  1079. atomic_set(&pst_adpd->stats.fifo_bytes[2], 0);
  1080. atomic_set(&pst_adpd->stats.fifo_bytes[3], 0);
  1081. ewma_init(&pst_adpd->stats.wq_schedule_time_avg_usec, 2048, 128);
  1082. ewma_init(&pst_adpd->stats.data_process_time_avg_usec, 2048, 128);
  1083. }
  1084. /* SAMPLE - SYSFS ATTRIBUTE*/
  1085. /**
  1086. This function is used for getting the status of the sample enable bit
  1087. @param dev linux device structure
  1088. @param attr pointer point linux device_attribute structure
  1089. @param buf pointer point the buffer
  1090. @return ssize_t size of data presnt in the buffer
  1091. */
  1092. static ssize_t
  1093. sample_attr_get_enable(struct device *dev,
  1094. struct device_attribute *attr, char *buf)
  1095. {
  1096. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1097. int val = atomic_read(&pst_adpd->sample_enabled);
  1098. return sprintf(buf, "%d\n", val);
  1099. }
  1100. /**
  1101. This function is used for enabling the Sample mode
  1102. @param dev linux device structure
  1103. @param attr pointer point linux device_attribute structure
  1104. @param buf pointer point the buffer
  1105. @param size buffer size
  1106. @return ssize_t size of data written to the buffer
  1107. */
  1108. static ssize_t
  1109. sample_attr_set_enable(struct device *dev, struct device_attribute *attr,
  1110. const char *buf, size_t size)
  1111. {
  1112. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1113. unsigned short parse_data[2];
  1114. unsigned short mode = 0;
  1115. unsigned short osc_trim_32k, osc_trim_32m;
  1116. unsigned short osc_trim_addr26 = 0, osc_trim_addr28 = 0, osc_trim_addr29 = 0;
  1117. int err;
  1118. int val;
  1119. int n = sscanf(buf, "%d", &val);
  1120. (void)n;
  1121. memset(parse_data, 0, sizeof(parse_data));
  1122. if (pst_adpd->osc_trim_open_enable == 1) {
  1123. err = osc_trim_efs_register_open(pst_adpd);
  1124. if (err < 0) {
  1125. pr_err("adpd142_%s: osc_trim_efs_register_open() empty(%d)\n", __func__, err);
  1126. osc_trim_32k = OSC_DEFAULT_32K_SET;
  1127. osc_trim_32m = OSC_DEFAULT_32M_SET;
  1128. } else {
  1129. osc_trim_32k = (unsigned short)pst_adpd->osc_trim_32K_value;
  1130. osc_trim_32m = (unsigned short)pst_adpd->osc_trim_32M_value;
  1131. osc_trim_addr26 = (unsigned short)pst_adpd->osc_trim_addr26_value;
  1132. osc_trim_addr28 = (unsigned short)pst_adpd->osc_trim_addr28_value;
  1133. osc_trim_addr29 = (unsigned short)pst_adpd->osc_trim_addr29_value;
  1134. reg_write(pst_adpd, OSC_TRIM_ADDR26_REG, osc_trim_addr26);
  1135. reg_write(pst_adpd, OSC_TRIM_ADDR28_REG, osc_trim_addr28);
  1136. reg_write(pst_adpd, OSC_TRIM_ADDR29_REG, osc_trim_addr29);
  1137. }
  1138. pr_info("adpd142_%s = 32K[%02x], 32M[%02x], addr26[%02x], addr28[%02x], addr29[%02x]\n",
  1139. __func__, osc_trim_32k, osc_trim_32m, osc_trim_addr26, osc_trim_addr28, osc_trim_addr29);
  1140. reg_write(pst_adpd, OSC_TRIM_32K_REG, osc_trim_32k);
  1141. reg_write(pst_adpd, OSC_TRIM_32M_REG, osc_trim_32m);
  1142. pst_adpd->osc_trim_open_enable = 0;
  1143. }
  1144. if (val == 1) {
  1145. pr_info("adpd142_%s_enable.\n", __func__);
  1146. if (!IS_ERR(pst_adpd->vdd_led))
  1147. regulator_enable(pst_adpd->vdd_led);
  1148. cmd_parsing("0x31", 1, parse_data);
  1149. atomic_set(&pst_adpd->sample_enabled, 1);
  1150. } else {
  1151. if (!IS_ERR(pst_adpd->vdd_led))
  1152. regulator_disable(pst_adpd->vdd_led);
  1153. cmd_parsing("0x0", 1, parse_data);
  1154. atomic_set(&pst_adpd->sample_enabled, 0);
  1155. }
  1156. mode = GET_USR_MODE(parse_data[0]);
  1157. if (GET_USR_MODE(parse_data[0]) < MAX_MODE) {
  1158. if ((GET_USR_SUB_MODE(parse_data[0])) <
  1159. __mode_recv_frm_usr[mode].size) {
  1160. adpd142_mode_switching(pst_adpd, parse_data[0]);
  1161. } else {
  1162. ADPD142_dbg("Sub mode Out of bound\n");
  1163. adpd142_mode_switching(pst_adpd, 0);
  1164. }
  1165. } else {
  1166. ADPD142_dbg("Mode out of bound\n");
  1167. adpd142_mode_switching(pst_adpd, 0);
  1168. }
  1169. return size;
  1170. }
  1171. /* GENERAL - SYSFS ATTRIBUTE*/
  1172. /**
  1173. This function is used for getting the status of the adpd_mode
  1174. @param dev linux device structure
  1175. @param attr pointer point linux device_attribute structure
  1176. @param buf pointer point the buffer
  1177. @return ssize_t size of data presnt in the buffer
  1178. */
  1179. static ssize_t
  1180. attr_get_mode(struct device *dev, struct device_attribute *attr, char *buf)
  1181. {
  1182. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1183. int val = atomic_read(&pst_adpd->adpd_mode);
  1184. return sprintf(buf, "%d\n", val);
  1185. }
  1186. /**
  1187. This function is used for switching ADPD142 mode
  1188. @param dev linux device structure
  1189. @param attr pointer point linux device_attribute structure
  1190. @param buf pointer point the buffer
  1191. @param size buffer size
  1192. @return ssize_t size of data written to the buffer
  1193. */
  1194. static ssize_t
  1195. attr_set_mode(struct device *dev, struct device_attribute *attr,
  1196. const char *buf, size_t size)
  1197. {
  1198. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1199. unsigned short parse_data[2];
  1200. unsigned short mode = 0;
  1201. memset(parse_data, 0, sizeof(parse_data));
  1202. cmd_parsing(buf, 1, parse_data);
  1203. ADPD142_info("Mode requested 0x%02x\n", parse_data[0]);
  1204. pr_info("adpd142_%s = data0[%02x], data1[%02x]\n", __func__, parse_data[0], parse_data[1]);
  1205. mode = GET_USR_MODE(parse_data[0]);
  1206. if (GET_USR_MODE(parse_data[0]) < MAX_MODE) {
  1207. if ((GET_USR_SUB_MODE(parse_data[0])) <
  1208. __mode_recv_frm_usr[mode].size) {
  1209. adpd142_mode_switching(pst_adpd, parse_data[0]);
  1210. } else {
  1211. ADPD142_dbg("Sub mode Out of bound\n");
  1212. adpd142_mode_switching(pst_adpd, 0);
  1213. }
  1214. } else {
  1215. ADPD142_dbg("Mode out of bound\n");
  1216. adpd142_mode_switching(pst_adpd, 0);
  1217. }
  1218. return size;
  1219. }
  1220. /**
  1221. This function is used for reading the register value
  1222. @param dev linux device structure
  1223. @param attr pointer point linux device_attribute structure
  1224. @param buf pointer point the buffer
  1225. @return ssize_t size of data presnt in the buffer
  1226. */
  1227. static ssize_t
  1228. attr_reg_read_get(struct device *dev, struct device_attribute *attr, char *buf)
  1229. {
  1230. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1231. ADPD142_dbg("Regval: 0x%4x\n", pst_adpd->sysfslastreadval);
  1232. return sprintf(buf, "0x%04x\n", pst_adpd->sysfslastreadval);
  1233. }
  1234. /**
  1235. This function is used for writing the register for reading back
  1236. @param dev linux device structure
  1237. @param attr pointer point linux device_attribute structure
  1238. @param buf pointer point the buffer
  1239. @param size buffer size
  1240. @return ssize_t size of data written to the buffer
  1241. */
  1242. static ssize_t
  1243. attr_reg_read_set(struct device *dev, struct device_attribute *attr,
  1244. const char *buf, size_t size)
  1245. {
  1246. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1247. unsigned short addr, cnt;
  1248. unsigned short parse_data[4];
  1249. unsigned short ret;
  1250. memset(parse_data, 0, sizeof(unsigned short) * 4);
  1251. cmd_parsing(buf, 2, parse_data);
  1252. addr = parse_data[0];
  1253. cnt = parse_data[1];
  1254. mutex_lock(&pst_adpd->mutex);
  1255. pst_adpd->sysfs_I2C_regaddr = addr;
  1256. ret = adpd142_sysfs_I2C_read(pst_adpd);
  1257. if (ret != (-1)) {
  1258. ADPD142_dbg("RegRead_Store: addr = 0x%04X,value = 0x%04X\n",
  1259. addr, ret);
  1260. pst_adpd->sysfslastreadval = ret;
  1261. } else {
  1262. ADPD142_dbg("%s Error\n", __func__);
  1263. pst_adpd->sysfslastreadval = (unsigned short) -1;
  1264. }
  1265. mutex_unlock(&pst_adpd->mutex);
  1266. return size;
  1267. }
  1268. void adpd_power_enable(struct adpd142_data *info, bool onoff)
  1269. {
  1270. int ret;
  1271. pr_info("%s: %s\n", __func__, onoff ? "on" : "off");
  1272. if (!info->vdd_led) {
  1273. info->vdd_led = regulator_get(&info->client->dev,"adpd_led");
  1274. if (IS_ERR(info->vdd_led)) {
  1275. dev_err(&info->client->dev,"Regulator(adpd_led) get failed rc = %ld\n", PTR_ERR(info->vdd_led));
  1276. return;
  1277. }
  1278. ret = regulator_set_voltage(info->vdd_led,3300000, 3300000);
  1279. if (ret) {
  1280. dev_err(&info->client->dev,
  1281. "regulator(vdd_led) set_vtg failed rc=%d\n", ret);
  1282. return;
  1283. }
  1284. }
  1285. if (!info->reg_adpd) {
  1286. info->reg_adpd = regulator_get(&info->client->dev,"adpd_vreg");
  1287. if (IS_ERR(info->reg_adpd)) {
  1288. dev_err(&info->client->dev,"Regulator(adpd_vreg) get failed rc = %ld\n", PTR_ERR(info->reg_adpd));
  1289. return;
  1290. }
  1291. }
  1292. if (onoff) {
  1293. if (!regulator_is_enabled(info->vdd_led)) {
  1294. ret = regulator_enable(info->vdd_led);
  1295. if (ret) {
  1296. pr_err("%s: enable vdd_led failed, rc=%d\n",__func__, ret);
  1297. return;
  1298. }
  1299. pr_info("%sHRM_LED 3.3V[vdd_led] on is finished.\n",__func__);
  1300. } else
  1301. pr_info("%sHRM_LED 3.3V[vdd_led] is already on.\n",__func__);
  1302. if (!regulator_is_enabled(info->reg_adpd)) {
  1303. ret = regulator_enable(info->reg_adpd);
  1304. if (ret) {
  1305. pr_err("%s: enable adpd_vreg failed, rc=%d\n",__func__, ret);
  1306. return;
  1307. }
  1308. pr_info("%sHRM_adpd_vreg 1.8V[adpd_vreg] on is finished.\n",__func__);
  1309. } else
  1310. pr_info("%sHRM_adpd_vreg 1.8V[adpd_vreg] is already on.\n",__func__);
  1311. } else {
  1312. if (regulator_is_enabled(info->vdd_led)) {
  1313. ret = regulator_disable(info->vdd_led);
  1314. if (ret) {
  1315. pr_err("%s: disable vdd_led failed, rc=%d\n",
  1316. __func__, ret);
  1317. return;
  1318. }
  1319. pr_info("%sHRM_LED 3.3V[vdd_led] off is finished.\n",__func__);
  1320. } else
  1321. pr_info("%sHRM_LED 3.3V[vdd_led] is already off.\n",__func__);
  1322. if (regulator_is_enabled(info->reg_adpd)) {
  1323. ret = regulator_disable(info->reg_adpd);
  1324. if (ret) {
  1325. pr_err("%s: disable adpd_vreg failed, rc=%d\n",
  1326. __func__, ret);
  1327. return;
  1328. }
  1329. pr_info("%sHRM_adpd_vreg 1.8V[adpd_vreg] off is finished.\n",__func__);
  1330. } else
  1331. pr_info("%sHRM_adpd_vreg 1.8V[adpd_vreg] is already off.\n",__func__);
  1332. }
  1333. }
  1334. static int adpd_parse_dt(struct adpd142_data *data, struct device *dev)
  1335. {
  1336. struct device_node *this_node= dev->of_node;
  1337. enum of_gpio_flags flags;
  1338. int rc;
  1339. if (this_node == NULL)
  1340. return -ENODEV;
  1341. data->hrm_int = of_get_named_gpio_flags(this_node,"adpd142,irq_gpio", 0, &flags);
  1342. pr_err("%s : get hrm_int(data->hrm_int)(%d) \n", __func__, data->hrm_int);
  1343. if (data->hrm_int < 0) {
  1344. pr_err("%s : get hrm_int(%d) error\n", __func__, data->hrm_int);
  1345. return -ENODEV;
  1346. }
  1347. rc = gpio_request(data->hrm_int, "hrm_int");
  1348. if (rc) {
  1349. pr_err("%s - failed to request hrm_int\n", __func__);
  1350. goto err_int_gpio_req;
  1351. }
  1352. rc = gpio_direction_input(data->hrm_int);
  1353. if (rc) {
  1354. pr_err("%s - failed to set hrm_int as input\n", __func__);
  1355. goto err_int_gpio_direction_input;
  1356. }
  1357. return 0;
  1358. err_int_gpio_direction_input:
  1359. gpio_free(data->hrm_int);
  1360. err_int_gpio_req:
  1361. return rc;
  1362. }
  1363. /**
  1364. This function is used for writing a particular data to the register
  1365. @param dev linux device structure
  1366. @param attr pointer point linux device_attribute structure
  1367. @param buf pointer point the buffer
  1368. @param size buffer size
  1369. @return ssize_t size of data written to the buffer
  1370. */
  1371. static ssize_t
  1372. attr_reg_write_set(struct device *dev, struct device_attribute *attr,
  1373. const char *buf, size_t size)
  1374. {
  1375. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1376. unsigned short cnt;
  1377. unsigned short parse_data[4];
  1378. unsigned short ret;
  1379. memset(parse_data, 0, sizeof(unsigned short) * 4);
  1380. cmd_parsing(buf, 3, parse_data);
  1381. if (parse_data[1] != 1) {
  1382. ADPD142_dbg("few many argument!!\n");
  1383. goto err_reg_write_argument;
  1384. }
  1385. pst_adpd->sysfs_I2C_regaddr = parse_data[0];
  1386. cnt = parse_data[1];
  1387. pst_adpd->sysfs_I2C_regval = parse_data[2];
  1388. mutex_lock(&pst_adpd->mutex);
  1389. ret = adpd142_sysfs_I2C_write(pst_adpd);
  1390. if (ret == pst_adpd->sysfs_I2C_regval) {
  1391. ADPD142_dbg("Reg[0x%04x] = 0x%04x\n",
  1392. pst_adpd->sysfs_I2C_regaddr,
  1393. pst_adpd->sysfs_I2C_regval);
  1394. } else {
  1395. ADPD142_dbg("Reg write error!!\n");
  1396. }
  1397. adpd142_update_config(pst_adpd);
  1398. mutex_unlock(&pst_adpd->mutex);
  1399. err_reg_write_argument:
  1400. return size;
  1401. }
  1402. /**
  1403. This function is used for getting the status of configuration
  1404. TBD
  1405. @param dev linux device structure
  1406. @param attr pointer point linux device_attribute structure
  1407. @param buf pointer point the buffer
  1408. @return ssize_t size of data presnt in the buffer
  1409. */
  1410. static ssize_t
  1411. attr_config_get(struct device *dev, struct device_attribute *attr, char *buf)
  1412. {
  1413. return sprintf(buf, "status of config thread\n");
  1414. }
  1415. /**
  1416. This function is used for wrting the configuration value to the register
  1417. 0 - write the configuration data present in file to ADPD142
  1418. 1 - write the configuration data present in Array to ADPD142
  1419. @param dev linux device structure
  1420. @param attr pointer point linux device_attribute structure
  1421. @param buf pointer point the buffer
  1422. @param size buffer size
  1423. @return ssize_t size of data written to the buffer
  1424. */
  1425. static ssize_t
  1426. attr_config_set(struct device *dev, struct device_attribute *attr,
  1427. const char *buf, size_t size)
  1428. {
  1429. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1430. unsigned short parse_data[1];
  1431. memset(parse_data, 0, sizeof(unsigned short) * 1);
  1432. cmd_parsing(buf, 1, parse_data);
  1433. if (parse_data[0] == FRM_ARR)
  1434. adpd142_configuration(pst_adpd, FRM_ARR);
  1435. else if (parse_data[0] == FRM_FILE)
  1436. adpd142_configuration(pst_adpd, FRM_FILE);
  1437. else
  1438. ADPD142_dbg("set 1 to config\n");
  1439. return size;
  1440. }
  1441. /**
  1442. This function is used for getting the status of adpd142 and driver
  1443. @param dev linux device structure
  1444. @param attr pointer point linux device_attribute structure
  1445. @param buf pointer point the buffer
  1446. @return ssize_t size of data presnt in the buffer
  1447. */
  1448. static ssize_t
  1449. attr_stat_get(struct device *dev, struct device_attribute *attr, char *buf)
  1450. {
  1451. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1452. unsigned int interrupts = atomic_read(&pst_adpd->stats.interrupts);
  1453. unsigned int wq_pending = atomic_read(&pst_adpd->stats.wq_pending);
  1454. unsigned int wq_schedule_time_peak_usec =
  1455. atomic_read(&pst_adpd->stats.wq_schedule_time_peak_usec);
  1456. unsigned int wq_schedule_time_last_usec =
  1457. atomic_read(&pst_adpd->stats.wq_schedule_time_last_usec);
  1458. unsigned int data_process_time_peak_usec =
  1459. atomic_read(&pst_adpd->stats.data_process_time_peak_usec);
  1460. unsigned int data_process_time_last_usec =
  1461. atomic_read(&pst_adpd->stats.data_process_time_last_usec);
  1462. return sprintf(buf, "\
  1463. interrupts : %u\n\
  1464. wq_pending : %u\n\
  1465. wq_schedule_time_peak_usec : %u\n\
  1466. wq_schedule_time_avg_usec : %d\n\
  1467. wq_schedule_time_last_usec : %u\n\
  1468. data_process_time_peak_usec : %u\n\
  1469. data_process_time_avg_usec : %d\n\
  1470. data_process_time_last_usec : %u\n\
  1471. fifo_requires_sync : %d\n\
  1472. fifo bytes history : [%d %d %d %d]\n\
  1473. ADPD142 driver version : %s\n\
  1474. ADPD142 Release date : %s\n",
  1475. interrupts, wq_pending,
  1476. wq_schedule_time_peak_usec,
  1477. (int)ewma_read(&pst_adpd->stats.wq_schedule_time_avg_usec),
  1478. wq_schedule_time_last_usec,
  1479. data_process_time_peak_usec,
  1480. (int)ewma_read(&pst_adpd->stats.data_process_time_avg_usec),
  1481. data_process_time_last_usec,
  1482. atomic_read(&pst_adpd->stats.fifo_requires_sync),
  1483. atomic_read(&pst_adpd->stats.fifo_bytes[0]),
  1484. atomic_read(&pst_adpd->stats.fifo_bytes[1]),
  1485. atomic_read(&pst_adpd->stats.fifo_bytes[2]),
  1486. atomic_read(&pst_adpd->stats.fifo_bytes[3]),
  1487. ADPD142_VERSION,
  1488. ADPD142_RELEASE_DATE);
  1489. }
  1490. #define ADPD142_STAT_RESET 1
  1491. /**
  1492. This function is used for wrting the adpd stat value to zero
  1493. @param dev linux device structure
  1494. @param attr pointer point linux device_attribute structure
  1495. @param buf pointer point the buffer
  1496. @param size buffer size
  1497. @return ssize_t size of data written to the buffer
  1498. */
  1499. static ssize_t
  1500. attr_stat_set(struct device *dev, struct device_attribute *attr,
  1501. const char *buf, size_t size)
  1502. {
  1503. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1504. unsigned short parse_data[1];
  1505. memset(parse_data, 0, sizeof(unsigned short) * 1);
  1506. cmd_parsing(buf, 1, parse_data);
  1507. if (parse_data[0] == ADPD142_STAT_RESET) {
  1508. ADPD142_dbg("Resetting statistics\n");
  1509. adpd_stat_reset(pst_adpd);
  1510. }
  1511. return size;
  1512. }
  1513. /**
  1514. array of sample attributes
  1515. */
  1516. static struct device_attribute sample_attributes[] = {
  1517. __ATTR(enable, 0664, sample_attr_get_enable, sample_attr_set_enable),
  1518. };
  1519. /**
  1520. This function is used for creating sysfs attribute for sample
  1521. @param dev linux device structure
  1522. @return int status of attribute creation
  1523. */
  1524. static int
  1525. create_sysfs_interfaces_sample(struct device *dev)
  1526. {
  1527. int i;
  1528. for (i = 0; i < ARRAY_SIZE(sample_attributes); i++)
  1529. if (device_create_file(dev, sample_attributes + i))
  1530. goto err_sysfs_interface_sample;
  1531. return 0;
  1532. err_sysfs_interface_sample:
  1533. for (; i >= 0; i--)
  1534. device_remove_file(dev, sample_attributes + i);
  1535. dev_err(dev, "%s:Unable to create interface\n", __func__);
  1536. return -1;
  1537. }
  1538. /**
  1539. This function is used for removing sysfs attribute for sample
  1540. @param dev linux device structure
  1541. @return void
  1542. */
  1543. static void
  1544. remove_sysfs_interfaces_sample(struct device *dev)
  1545. {
  1546. int i;
  1547. for (i = 0; i < ARRAY_SIZE(sample_attributes); i++)
  1548. device_remove_file(dev, sample_attributes + i);
  1549. }
  1550. /**
  1551. This function is used for creating sysfs attribute
  1552. @param dev linux device structure
  1553. @return int status of attribute creation
  1554. */
  1555. static int
  1556. create_sysfs_interfaces(struct device *dev)
  1557. {
  1558. return 0;
  1559. }
  1560. /**
  1561. This function is used for removing sysfs attribute
  1562. @param dev linux device structure
  1563. @return void
  1564. */
  1565. static void
  1566. remove_sysfs_interfaces(struct device *dev)
  1567. {
  1568. }
  1569. /**
  1570. This function is used for registering input event for Sample
  1571. @param pst_adpd pointer point ADPD142 data structure
  1572. @return s32 status of this function
  1573. */
  1574. static s32
  1575. adpd142_input_init_sample(struct adpd142_data *pst_adpd)
  1576. {
  1577. int err;
  1578. pst_adpd->ptr_sample_inputdev = input_allocate_device();
  1579. if (!pst_adpd->ptr_sample_inputdev) {
  1580. err = -ENOMEM;
  1581. dev_err(&pst_adpd->client->dev, "input dev allocation fail\n");
  1582. goto err_sample_allocate;
  1583. }
  1584. pst_adpd->ptr_sample_inputdev->name = MODULE_NAME_HRM;
  1585. input_set_drvdata(pst_adpd->ptr_sample_inputdev, pst_adpd);
  1586. __set_bit(EV_MSC, pst_adpd->ptr_sample_inputdev->evbit);
  1587. __set_bit(EV_REL, pst_adpd->ptr_sample_inputdev->evbit);
  1588. __set_bit(MSC_RAW, pst_adpd->ptr_sample_inputdev->mscbit);
  1589. __set_bit(REL_X, pst_adpd->ptr_sample_inputdev->relbit);
  1590. __set_bit(REL_Y, pst_adpd->ptr_sample_inputdev->relbit);
  1591. __set_bit(REL_Z, pst_adpd->ptr_sample_inputdev->relbit);
  1592. err = input_register_device(pst_adpd->ptr_sample_inputdev);
  1593. if (err) {
  1594. dev_err(&pst_adpd->client->dev,
  1595. "unable to register input dev %s\n",
  1596. pst_adpd->ptr_sample_inputdev->name);
  1597. goto err_sample_register_failed;
  1598. }
  1599. err = sensors_create_symlink(&pst_adpd->ptr_sample_inputdev->dev.kobj,
  1600. pst_adpd->ptr_sample_inputdev->name);
  1601. if (err < 0) {
  1602. pr_err("[HRM] %s: create_symlink error\n", __func__);
  1603. goto err_sample_register_failed;
  1604. }
  1605. return 0;
  1606. err_sample_register_failed:
  1607. input_free_device(pst_adpd->ptr_sample_inputdev);
  1608. err_sample_allocate:
  1609. return err;
  1610. }
  1611. /**
  1612. This function is used for unregistering input event for Sample
  1613. @param pst_adpd pointer point ADPD142 data structure
  1614. @return void
  1615. */
  1616. static void
  1617. adpd142_input_cleanup_sample(struct adpd142_data *pst_adpd)
  1618. {
  1619. input_set_drvdata(pst_adpd->ptr_sample_inputdev, NULL);
  1620. input_unregister_device(pst_adpd->ptr_sample_inputdev);
  1621. input_free_device(pst_adpd->ptr_sample_inputdev);
  1622. }
  1623. /**
  1624. This function is used for registering input event for ADPD142
  1625. @param pst_adpd pointer point ADPD142 data structure
  1626. @return s32 status of the function
  1627. */
  1628. static s32
  1629. adpd142_input_init(struct adpd142_data *pst_adpd)
  1630. {
  1631. return adpd142_input_init_sample(pst_adpd);
  1632. }
  1633. /**
  1634. This function is used for unregistering input event done for ADPD142
  1635. @param pst_adpd pointer point ADPD142 data structure
  1636. @return void
  1637. */
  1638. static void
  1639. adpd142_input_cleanup(struct adpd142_data *pst_adpd)
  1640. {
  1641. adpd142_input_cleanup_sample(pst_adpd);
  1642. }
  1643. /**
  1644. This function is used for registering sysfs attribute for ADPD142
  1645. @param pst_adpd pointer point ADPD142 data structure
  1646. @return s32 status of the called function
  1647. */
  1648. static s32
  1649. adpd142_sysfs_init(struct adpd142_data *pst_adpd)
  1650. {
  1651. if (create_sysfs_interfaces(&pst_adpd->client->dev))
  1652. goto err_sysfs_create_gen;
  1653. if (create_sysfs_interfaces_sample(&pst_adpd->ptr_sample_inputdev->dev))
  1654. goto err_sysfs_create_sample;
  1655. return 0;
  1656. err_sysfs_create_sample:
  1657. remove_sysfs_interfaces(&pst_adpd->client->dev);
  1658. err_sysfs_create_gen:
  1659. return -1;
  1660. }
  1661. /**
  1662. This function is used for unregistering sysfs attribute for ADPD142
  1663. @param pst_adpd pointer point ADPD142 data structure
  1664. @return void
  1665. */
  1666. static void
  1667. adpd142_sysfs_cleanup(struct adpd142_data *pst_adpd)
  1668. {
  1669. remove_sysfs_interfaces(&pst_adpd->client->dev);
  1670. remove_sysfs_interfaces_sample(&pst_adpd->ptr_sample_inputdev->dev);
  1671. }
  1672. /**
  1673. This function is used for assigning initial assignment value to
  1674. ADPD142 data structure
  1675. @param pst_adpd pointer point ADPD142 data structure
  1676. @return void
  1677. */
  1678. static void
  1679. adpd142_struct_assign(struct adpd142_data *pst_adpd)
  1680. {
  1681. pst_adpd->ptr_data_buffer = data_buffer;
  1682. }
  1683. /**
  1684. This function is used for initializing ADPD142
  1685. @param pst_adpd pointer point ADPD142 data structure
  1686. @param id pointer point i2c device id
  1687. @return s32 status of the called function
  1688. */
  1689. static s32
  1690. adpd142_initialization(struct adpd142_data *pst_adpd,
  1691. const struct i2c_device_id *id)
  1692. {
  1693. int err = 0;
  1694. if (adpd142_input_init(pst_adpd)) {
  1695. err = -1;
  1696. pr_err("%s err line %d\n", __func__, __LINE__);
  1697. goto err_input_init;
  1698. }
  1699. if (adpd142_sysfs_init(pst_adpd)) {
  1700. pr_err("%s err line %d\n", __func__, __LINE__);
  1701. err = -1;
  1702. goto err_sysfs_init;
  1703. }
  1704. adpd142_struct_assign(pst_adpd);
  1705. memset(&pst_adpd->stats, 0, sizeof(pst_adpd->stats));
  1706. adpd_stat_reset(pst_adpd);
  1707. INIT_WORK(&pst_adpd->work, adpd142_wq_handler);
  1708. pst_adpd->ptr_adpd142_wq_st =
  1709. create_workqueue("adpd142_wq");
  1710. if (!pst_adpd->ptr_adpd142_wq_st) {
  1711. err = -ENOMEM;
  1712. pr_err("%s %d\n", __func__, __LINE__);
  1713. goto err_wq_creation_init;
  1714. }
  1715. if (!pst_adpd->client->irq){
  1716. pr_err("%s %d\n", __func__, __LINE__);
  1717. goto err_work_queue_init;
  1718. }
  1719. pst_adpd->hrm_int = pst_adpd->client->irq;
  1720. pst_adpd->irq = pst_adpd->hrm_int;
  1721. irq_set_irq_type(pst_adpd->irq, IRQ_TYPE_EDGE_RISING);
  1722. err = request_irq(pst_adpd->irq, adpd142_isr_handler,
  1723. IRQF_TRIGGER_RISING, dev_name(&pst_adpd->client->dev),
  1724. pst_adpd);
  1725. if (err) {
  1726. ADPD142_dbg("irq %d busy?\n", pst_adpd->irq);
  1727. goto err_work_queue_init;
  1728. }
  1729. disable_irq_nosync(pst_adpd->irq);
  1730. pst_adpd->ptr_config = kzalloc(sizeof(struct adpd_platform_data),
  1731. GFP_KERNEL);
  1732. if (pst_adpd->ptr_config == NULL) {
  1733. err = -ENOMEM;
  1734. pr_err("%s %d\n", __func__, __LINE__);
  1735. goto err_work_queue_init;
  1736. }
  1737. enable_irq(pst_adpd->irq);
  1738. adpd142_configuration(pst_adpd, 1);
  1739. adpd142_mode_switching(pst_adpd, 0); /* turn off : idle mode, temp*/
  1740. pst_adpd->osc_trim_open_enable = 1;
  1741. return 0;
  1742. //err_gpio_direction_input:
  1743. //gpio_free(pst_adpd->hrm_int);
  1744. err_work_queue_init:
  1745. destroy_workqueue(pst_adpd->ptr_adpd142_wq_st);
  1746. err_wq_creation_init:
  1747. err_sysfs_init:
  1748. adpd142_input_cleanup(pst_adpd);
  1749. err_input_init:
  1750. return err;
  1751. }
  1752. /**
  1753. This function is used for cleanup ADPD142
  1754. @param pst_adpd pointer point ADPD142 data structure
  1755. @return void
  1756. */
  1757. static void
  1758. adpd142_initialization_cleanup(struct adpd142_data *pst_adpd)
  1759. {
  1760. adpd142_mode_switching(pst_adpd, 0);
  1761. free_irq(pst_adpd->irq, pst_adpd);
  1762. destroy_workqueue(pst_adpd->ptr_adpd142_wq_st);
  1763. adpd142_sysfs_cleanup(pst_adpd);
  1764. adpd142_input_cleanup(pst_adpd);
  1765. kobject_uevent(&pst_adpd->client->dev.kobj, KOBJ_OFFLINE);
  1766. }
  1767. #ifdef CONFIG_PM
  1768. static s32
  1769. adpd142_i2c_suspend(struct device *dev)
  1770. {
  1771. return 0;
  1772. }
  1773. static s32
  1774. adpd142_i2c_resume(struct device *dev)
  1775. {
  1776. return 0;
  1777. }
  1778. #else
  1779. #define adpd142_i2c_resume NULL
  1780. #define adpd142_i2c_suspend NULL
  1781. #endif /* CONFIG_PM */
  1782. /**
  1783. This function is used for i2c read communication between ADPD142 and AP
  1784. @param pst_adpd pointer point ADPD142 data structure
  1785. @param reg_addr address need to be fetch
  1786. @param len number byte to be read
  1787. @param buf pointer point the read out data.
  1788. @return s32 status of the called function
  1789. */
  1790. static int
  1791. adpd142_i2c_read(struct adpd142_data *pst_adpd, u8 reg_addr, int len,
  1792. u16 *buf)
  1793. {
  1794. int err;
  1795. int tries = 0;
  1796. int icnt = 0;
  1797. struct i2c_msg msgs[] = {
  1798. {
  1799. .addr = pst_adpd->client->addr,
  1800. .flags = pst_adpd->client->flags & I2C_M_TEN,
  1801. .len = 1,
  1802. .buf = (s8 *)&reg_addr,
  1803. },
  1804. {
  1805. .addr = pst_adpd->client->addr,
  1806. .flags = (pst_adpd->client->flags & I2C_M_TEN) |
  1807. I2C_M_RD,
  1808. .len = len * sizeof(unsigned short),
  1809. .buf = (s8 *)buf,
  1810. },
  1811. };
  1812. do {
  1813. err = i2c_transfer(pst_adpd->client->adapter, msgs, 2);
  1814. if (err != 2)
  1815. msleep_interruptible(I2C_RETRY_DELAY);
  1816. } while ((err != 2) && (++tries < I2C_RETRIES));
  1817. if (err != 2) {
  1818. dev_err(&pst_adpd->client->dev, "read transfer error\n");
  1819. err = -EIO;
  1820. } else {
  1821. err = 0;
  1822. }
  1823. for (icnt = 0; icnt < len; icnt++) {
  1824. /*convert big endian to CPU format*/
  1825. buf[icnt] = be16_to_cpu(buf[icnt]);
  1826. }
  1827. return err;
  1828. }
  1829. /**
  1830. This function is used for i2c write communication between ADPD142 and AP
  1831. @param pst_adpd pointer point ADPD142 data structure
  1832. @param reg_addr address need to be fetch
  1833. @param len number byte to be read
  1834. @param data value to be written on the register.
  1835. @return s32 status of the called function
  1836. */
  1837. static int
  1838. adpd142_i2c_write(struct adpd142_data *pst_adpd, u8 reg_addr, int len,
  1839. u16 data)
  1840. {
  1841. struct i2c_msg msgs[1];
  1842. int err;
  1843. int tries = 0;
  1844. unsigned short data_to_write = cpu_to_be16(data);
  1845. char buf[4];
  1846. buf[0] = (s8) reg_addr;
  1847. memcpy(buf + 1, &data_to_write, sizeof(unsigned short));
  1848. msgs[0].addr = pst_adpd->client->addr;
  1849. msgs[0].flags = pst_adpd->client->flags & I2C_M_TEN;
  1850. msgs[0].len = 1 + (1 * sizeof(unsigned short));
  1851. msgs[0].buf = buf;
  1852. do {
  1853. err = i2c_transfer(pst_adpd->client->adapter, msgs, 1);
  1854. if (err != 1)
  1855. msleep_interruptible(I2C_RETRY_DELAY);
  1856. } while ((err != 1) && (++tries < I2C_RETRIES));
  1857. if (err != 1) {
  1858. dev_err(&pst_adpd->client->dev, "write transfer error\n");
  1859. err = -EIO;
  1860. } else {
  1861. err = 0;
  1862. }
  1863. return err;
  1864. }
  1865. /* set sysfs for hrm sensor */
  1866. static ssize_t adpd142_name_show(struct device *dev,
  1867. struct device_attribute *attr, char *buf)
  1868. {
  1869. pr_info("%s = %s \n", __func__, CHIP_NAME);
  1870. return snprintf(buf, PAGE_SIZE, "%s\n", CHIP_NAME);
  1871. }
  1872. static ssize_t adpd142_vendor_show(struct device *dev,
  1873. struct device_attribute *attr, char *buf)
  1874. {
  1875. pr_info("%s = %s \n", __func__, VENDOR);
  1876. return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR);
  1877. }
  1878. static ssize_t eol_test_result_store(struct device *dev,
  1879. struct device_attribute *attr, const char *buf, size_t size);
  1880. static ssize_t eol_test_store(struct device *dev,
  1881. struct device_attribute *attr, const char *buf, size_t size)
  1882. {
  1883. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1884. if (sysfs_streq(buf, "1")) /* eol_test start */
  1885. pst_adpd->eol_test_is_enable = 1;
  1886. else if (sysfs_streq(buf, "0")) /* eol_test stop */
  1887. pst_adpd->eol_test_is_enable = 0;
  1888. else {
  1889. pr_debug("adpd142_%s: invalid value %d\n", __func__, *buf);
  1890. return -EINVAL;
  1891. }
  1892. pst_adpd->eol_test_status = 0;
  1893. pr_info("adpd142_%s = (%c), value(%u) \n", __func__, *buf, pst_adpd->eol_test_is_enable);
  1894. return size;
  1895. }
  1896. static ssize_t eol_test_show(struct device *dev,
  1897. struct device_attribute *attr, char *buf)
  1898. {
  1899. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1900. return snprintf(buf, PAGE_SIZE, "%u\n", pst_adpd->eol_test_is_enable);
  1901. }
  1902. static ssize_t eol_test_result_store(struct device *dev,
  1903. struct device_attribute *attr, const char *buf, size_t size)
  1904. {
  1905. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1906. int err;
  1907. unsigned int buf_len;
  1908. buf_len = strlen(buf) + 1;
  1909. if (buf_len > MAX_EOL_RESULT)
  1910. buf_len = MAX_EOL_RESULT;
  1911. if (pst_adpd->eol_test_result != NULL)
  1912. kfree(pst_adpd->eol_test_result);
  1913. pst_adpd->eol_test_result = kzalloc(sizeof(char) * buf_len, GFP_KERNEL);
  1914. if (pst_adpd->eol_test_result == NULL) {
  1915. pr_err("adpd142_%s - couldn't allocate memory\n", __func__);
  1916. return -ENOMEM;
  1917. }
  1918. strncpy(pst_adpd->eol_test_result, buf, buf_len);
  1919. pr_info("adpd142_%s - result = %s, buf_len(%u)\n", __func__, pst_adpd->eol_test_result, buf_len);
  1920. pst_adpd->eol_test_status = 1;
  1921. err = osc_trim_efs_register_save(pst_adpd);
  1922. if (err < 0) {
  1923. pr_err("adpd142_%s: osc_trim_efs_register_save() empty(%d)\n", __func__, err);
  1924. }
  1925. return size;
  1926. }
  1927. static ssize_t eol_test_result_show(struct device *dev,
  1928. struct device_attribute *attr, char *buf)
  1929. {
  1930. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1931. if (pst_adpd->eol_test_result == NULL) {
  1932. pr_info("adpd142_%s - data->eol_test_result is NULL\n", __func__);
  1933. pst_adpd->eol_test_status = 0;
  1934. return snprintf(buf, PAGE_SIZE, "%s\n", "NO_EOL_TEST");
  1935. }
  1936. pr_info("adpd142_%s - result = %s\n", __func__, pst_adpd->eol_test_result);
  1937. pst_adpd->eol_test_status = 0;
  1938. return snprintf(buf, PAGE_SIZE, "%s\n", pst_adpd->eol_test_result);
  1939. }
  1940. static ssize_t eol_test_status_show(struct device *dev,
  1941. struct device_attribute *attr, char *buf)
  1942. {
  1943. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1944. return snprintf(buf, PAGE_SIZE, "%u\n", pst_adpd->eol_test_status);
  1945. }
  1946. static ssize_t adpd142_eol_lib_ver_store(struct device *dev,
  1947. struct device_attribute *attr, const char *buf, size_t size)
  1948. {
  1949. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1950. unsigned int buf_len;
  1951. buf_len = strlen(buf) + 1;
  1952. if (buf_len > MAX_LIB_VER)
  1953. buf_len = MAX_LIB_VER;
  1954. if (pst_adpd->eol_lib_ver != NULL)
  1955. kfree(pst_adpd->eol_lib_ver);
  1956. pst_adpd->eol_lib_ver = kzalloc(sizeof(char) * buf_len, GFP_KERNEL);
  1957. if (pst_adpd->eol_lib_ver == NULL) {
  1958. pr_err("%s - couldn't allocate memory\n", __func__);
  1959. return -ENOMEM;
  1960. }
  1961. strncpy(pst_adpd->eol_lib_ver, buf, buf_len);
  1962. pr_info("%s - eol_lib_ver = %s\n", __func__, pst_adpd->eol_lib_ver);
  1963. return size;
  1964. }
  1965. static ssize_t adpd142_eol_lib_ver_show(struct device *dev,
  1966. struct device_attribute *attr, char *buf)
  1967. {
  1968. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1969. if (pst_adpd->eol_lib_ver == NULL) {
  1970. pr_info("%s - data->eol_lib_ver is NULL\n", __func__);
  1971. return snprintf(buf, PAGE_SIZE, "%s\n", "NULL");
  1972. }
  1973. pr_info("%s - eol_lib_ver = %s\n", __func__, pst_adpd->eol_lib_ver);
  1974. return snprintf(buf, PAGE_SIZE, "%s\n", pst_adpd->eol_lib_ver);
  1975. }
  1976. static ssize_t adpd142_elf_lib_ver_store(struct device *dev,
  1977. struct device_attribute *attr, const char *buf, size_t size)
  1978. {
  1979. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1980. unsigned int buf_len;
  1981. buf_len = strlen(buf) + 1;
  1982. if (buf_len > MAX_LIB_VER)
  1983. buf_len = MAX_LIB_VER;
  1984. if (pst_adpd->elf_lib_ver != NULL)
  1985. kfree(pst_adpd->elf_lib_ver);
  1986. pst_adpd->elf_lib_ver = kzalloc(sizeof(char) * buf_len, GFP_KERNEL);
  1987. if (pst_adpd->elf_lib_ver == NULL) {
  1988. pr_err("%s - couldn't allocate memory\n", __func__);
  1989. return -ENOMEM;
  1990. }
  1991. strncpy(pst_adpd->elf_lib_ver, buf, buf_len);
  1992. return size;
  1993. }
  1994. static ssize_t adpd142_elf_lib_ver_show(struct device *dev,
  1995. struct device_attribute *attr, char *buf)
  1996. {
  1997. struct adpd142_data *pst_adpd = dev_get_drvdata(dev);
  1998. if (pst_adpd->elf_lib_ver == NULL) {
  1999. pr_info("%s - data->elf_lib_ver is NULL\n", __func__);
  2000. return snprintf(buf, PAGE_SIZE, "%s\n", "NULL");
  2001. }
  2002. pr_info("%s - elf_lib_ver = %s\n", __func__, pst_adpd->elf_lib_ver);
  2003. return snprintf(buf, PAGE_SIZE, "%s\n", pst_adpd->elf_lib_ver);
  2004. }
  2005. static DEVICE_ATTR(name, S_IRUGO, adpd142_name_show, NULL);
  2006. static DEVICE_ATTR(vendor, S_IRUGO, adpd142_vendor_show, NULL);
  2007. static DEVICE_ATTR(eol_test, S_IRUGO | S_IWUSR | S_IWGRP,
  2008. eol_test_show, eol_test_store);
  2009. static DEVICE_ATTR(eol_test_result, S_IRUGO | S_IWUSR | S_IWGRP,
  2010. eol_test_result_show, eol_test_result_store);
  2011. static DEVICE_ATTR(eol_test_status, S_IRUGO, eol_test_status_show, NULL);
  2012. static DEVICE_ATTR(eol_lib_ver, S_IRUGO | S_IWUSR | S_IWGRP,
  2013. adpd142_eol_lib_ver_show, adpd142_eol_lib_ver_store);
  2014. static DEVICE_ATTR(elf_lib_ver, S_IRUGO | S_IWUSR | S_IWGRP,
  2015. adpd142_elf_lib_ver_show, adpd142_elf_lib_ver_store);
  2016. static DEVICE_ATTR(adpd_mode, S_IRUGO | S_IWUSR | S_IWGRP,
  2017. attr_get_mode, attr_set_mode);
  2018. static DEVICE_ATTR(adpd_reg_read, S_IRUGO | S_IWUSR | S_IWGRP,
  2019. attr_reg_read_get, attr_reg_read_set);
  2020. static DEVICE_ATTR(adpd_reg_write, S_IRUGO | S_IWUSR | S_IWGRP,
  2021. NULL, attr_reg_write_set);
  2022. static DEVICE_ATTR(adpd_configuration, S_IRUGO | S_IWUSR | S_IWGRP,
  2023. attr_config_get, attr_config_set);
  2024. static DEVICE_ATTR(adpd_stat, S_IRUGO | S_IWUSR | S_IWGRP,
  2025. attr_stat_get, attr_stat_set);
  2026. static struct device_attribute *hrm_sensor_attrs[] = {
  2027. &dev_attr_name,
  2028. &dev_attr_vendor,
  2029. &dev_attr_eol_test,
  2030. &dev_attr_eol_test_result,
  2031. &dev_attr_eol_test_status,
  2032. &dev_attr_eol_lib_ver,
  2033. &dev_attr_elf_lib_ver,
  2034. &dev_attr_adpd_mode,
  2035. &dev_attr_adpd_reg_read,
  2036. &dev_attr_adpd_reg_write,
  2037. &dev_attr_adpd_configuration,
  2038. &dev_attr_adpd_stat,
  2039. NULL,
  2040. };
  2041. /**
  2042. This function is used for ADPD142 probe function
  2043. @param client pointer point to the linux i2c client structure
  2044. @param id pointer point to the linux i2c device id
  2045. @return s32 status of the probe function
  2046. */
  2047. static s32
  2048. adpd_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  2049. {
  2050. struct adpd142_data *pst_adpd = NULL;
  2051. struct adpd_platform_data *pdata = NULL;
  2052. int err = 0;
  2053. unsigned short u16_regval = 0;
  2054. pr_err("%s, is called Success\n", __func__);
  2055. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  2056. dev_err(&client->dev, "client not i2c capable\n");
  2057. err = -ENODEV;
  2058. pr_err("[SENSOR] %s - exit_check_functionality.\n", __func__);
  2059. goto exit_check_functionality_failed;
  2060. }
  2061. pst_adpd = kzalloc(sizeof(struct adpd142_data), GFP_KERNEL);
  2062. if (pst_adpd == NULL) {
  2063. err = -ENOMEM;
  2064. pr_err("[SENSOR] %s - exit_mem_allocate.\n", __func__);
  2065. goto exit_mem_allocate_failed;
  2066. }
  2067. if(client->dev.of_node) {
  2068. pdata = devm_kzalloc (&client->dev ,
  2069. sizeof(struct adpd_platform_data), GFP_KERNEL);
  2070. if(!pdata) {
  2071. dev_err(&client->dev, "Failed to allocate memory\n");
  2072. if(pst_adpd)
  2073. kfree(pst_adpd);
  2074. return -ENOMEM;
  2075. }
  2076. } else
  2077. pdata = client->dev.platform_data;
  2078. if (!pdata) {
  2079. pr_err("%s: missing pdata!\n", __func__);
  2080. if(pst_adpd)
  2081. kfree(pst_adpd);
  2082. return err;
  2083. }
  2084. err = adpd_parse_dt(pst_adpd, &client->dev);
  2085. if (err) {
  2086. pr_err("%s, parse dt fail\n", __func__);
  2087. goto adpd_parse_dt_err;
  2088. }
  2089. mutex_init(&pst_adpd->mutex);
  2090. pst_adpd->client = client;
  2091. pst_adpd->client->irq = gpio_to_irq(pst_adpd->hrm_int);
  2092. pst_adpd->ptr_config = pdata;
  2093. pst_adpd->read = adpd142_i2c_read;
  2094. pst_adpd->write = adpd142_i2c_write;
  2095. /*Need to allocate and assign data then use the below function */
  2096. i2c_set_clientdata(client, (struct adpd142_data *)pst_adpd);
  2097. adpd_power_enable(pst_adpd, 1);
  2098. msleep(30);
  2099. /*chip ID verification */
  2100. u16_regval = reg_read(pst_adpd, ADPD_CHIPID_ADDR);
  2101. switch (u16_regval) {
  2102. case ADPD_CHIPID(0):
  2103. case ADPD_CHIPID(1):
  2104. case ADPD_CHIPID(2):
  2105. err = 0;
  2106. pr_err("chipID value is = 0x%x\n", u16_regval);
  2107. break;
  2108. default:
  2109. err = 1;
  2110. break;
  2111. };
  2112. if (err) {
  2113. pr_err("[SENSOR] %s - exit_chipid_verification.\n", __func__);
  2114. goto exit_chipid_verification;
  2115. }
  2116. //pst_adpd->dev = &client->dev;
  2117. pr_info("%s - start \n", __func__);
  2118. if (adpd142_initialization(pst_adpd, id)){
  2119. pr_err("[SENSOR] %s - exit_adpd142_init_exit.\n", __func__);
  2120. goto exit_adpd142_init;
  2121. }
  2122. /* set sysfs for hrm sensor */
  2123. err = sensors_register(pst_adpd->dev, pst_adpd, hrm_sensor_attrs,
  2124. "hrm_sensor");
  2125. if (err) {
  2126. pr_err("[SENSOR] %s - cound not register hrm_sensor(%d).\n",
  2127. __func__, err);
  2128. goto hrm_sensor_register_failed;
  2129. }
  2130. pr_info("%s success\n", __func__);
  2131. goto done;
  2132. hrm_sensor_register_failed:
  2133. exit_adpd142_init:
  2134. exit_chipid_verification:
  2135. mutex_destroy(&pst_adpd->mutex);
  2136. adpd_parse_dt_err:
  2137. exit_mem_allocate_failed:
  2138. kfree(pst_adpd);
  2139. exit_check_functionality_failed:
  2140. dev_err(&client->dev, "%s: Driver Init failed\n", ADPD_DEV_NAME);
  2141. pr_err("%s failed\n", __func__);
  2142. done:
  2143. return err;
  2144. }
  2145. /**
  2146. This function is used for ADPD142 remove function
  2147. @param client pointer point to the linux i2c client structure
  2148. @return s32 status of the remove function
  2149. */
  2150. static s32
  2151. adpd_i2c_remove(struct i2c_client *client)
  2152. {
  2153. struct adpd142_data *pst_adpd = i2c_get_clientdata(client);
  2154. ADPD142_dbg("%s\n", __func__);
  2155. adpd142_initialization_cleanup(pst_adpd);
  2156. gpio_free(pst_adpd->hrm_int);
  2157. kfree(pst_adpd->ptr_config);
  2158. kfree(pst_adpd);
  2159. pst_adpd = NULL;
  2160. i2c_set_clientdata(client, NULL);
  2161. return 0;
  2162. }
  2163. #ifdef CONFIG_PM
  2164. /**
  2165. device power management operation structure
  2166. */
  2167. static const struct dev_pm_ops adpd_pm_ops = {
  2168. .resume = adpd142_i2c_resume,
  2169. .suspend = adpd142_i2c_suspend,
  2170. };
  2171. #endif
  2172. /**
  2173. This table tell which framework it supported
  2174. @brief the name has to get matched to the board configuration file setup
  2175. */
  2176. /*static struct i2c_device_id adpd_id[] = { {ADPD_DEV_NAME, 0}, {} };
  2177. MODULE_DEVICE_TABLE(i2c, adpd_id);*/
  2178. static struct of_device_id adpd_match_table[] = {
  2179. { .compatible = "adpd142",},
  2180. {},
  2181. };
  2182. MODULE_DEVICE_TABLE(i2c, adpd_match_table);
  2183. static const struct i2c_device_id adpd142_device_id[] = {
  2184. { "adpd142", 0 },
  2185. { }
  2186. };
  2187. /**
  2188. i2c operation structure
  2189. */
  2190. struct i2c_driver adpd142_i2c_driver = {
  2191. .driver = {
  2192. .name = ADPD_DEV_NAME,
  2193. .owner = THIS_MODULE,
  2194. #ifdef CONFIG_PM
  2195. .pm = &adpd_pm_ops,
  2196. #endif
  2197. .of_match_table = adpd_match_table,
  2198. },
  2199. .probe = adpd_i2c_probe,
  2200. .remove = adpd_i2c_remove,
  2201. .id_table = adpd142_device_id,
  2202. };
  2203. static struct i2c_client *i2c_client;
  2204. #ifdef ADPD_AUTO_PROBE
  2205. static int
  2206. i2c_check_dev_attach(char *slave_name, unsigned short *slave_addrs,
  2207. unsigned short cnt)
  2208. {
  2209. struct i2c_board_info info;
  2210. struct i2c_adapter *i2c_adapter = NULL;
  2211. int ret = 0;
  2212. unsigned short *scan_device = NULL;
  2213. unsigned short count = 0;
  2214. /*need to check whether we need to free the memory */
  2215. scan_device = kzalloc(sizeof(unsigned short) * (cnt + 2), GFP_KERNEL);
  2216. if (IS_ERR(scan_device)) {
  2217. ret = -ENOMEM; /* out of memory */
  2218. goto i2c_check_attach_mem_fail;
  2219. }
  2220. memset(scan_device, '\0', sizeof(unsigned short) * (cnt + 2));
  2221. for (count = 0; count < cnt; count++) {
  2222. *(scan_device + count) = *(slave_addrs + count);
  2223. ADPD142_info("list of slave addr = 0x%x\n",
  2224. *(scan_device + count));
  2225. }
  2226. *(scan_device + count) = I2C_CLIENT_END;
  2227. count = 0;
  2228. do {
  2229. i2c_adapter = i2c_get_adapter(count);
  2230. if (i2c_adapter != NULL) {
  2231. memset(&info, 0, sizeof(struct i2c_board_info));
  2232. strlcpy(info.type, slave_name /*"adpd142" */ ,
  2233. I2C_NAME_SIZE);
  2234. /*need to check i2c_new_device instead of
  2235. i2c_new_probed_device*/
  2236. i2c_client =
  2237. i2c_new_probed_device(i2c_adapter, &info,
  2238. (const unsigned short *)
  2239. scan_device, NULL);
  2240. if (i2c_client != NULL) {
  2241. ADPD142_dbg("I2C busnum - %d\n", count);
  2242. ADPD142_dbg(
  2243. "device is attached to the bus i2c-%d\n",
  2244. count);
  2245. } else {
  2246. }
  2247. i2c_put_adapter(i2c_adapter);
  2248. } else {
  2249. ADPD142_info("Not valid adapter\n");
  2250. }
  2251. count++;
  2252. } while (i2c_client == NULL && count < 20);
  2253. kfree(scan_device);
  2254. if (i2c_client == NULL) {
  2255. /*No such device or address */
  2256. return -ENXIO;
  2257. } else {
  2258. return 0;
  2259. }
  2260. i2c_check_attach_mem_fail:
  2261. return ret;
  2262. }
  2263. #endif
  2264. /**
  2265. This function is get called when the module is inserted
  2266. @return inti status of the adpd142_multisensor_init
  2267. */
  2268. static int __init
  2269. adpd142_multisensor_init(void)
  2270. {
  2271. #ifdef ADPD_AUTO_PROBE
  2272. ADPD142_dbg("%s\n", __func__);
  2273. unsigned short addr[] = { ADPD142_SLAVE_ADDR };
  2274. if (!i2c_check_dev_attach(ADPD_DEV_NAME, addr, 1)) {
  2275. return i2c_add_driver(&adpd142_i2c_driver);
  2276. } else {
  2277. pr_err("i2c bus connect error\n");
  2278. return -1;
  2279. }
  2280. #else
  2281. ADPD142_dbg("%s\n", __func__);
  2282. return i2c_add_driver(&adpd142_i2c_driver);
  2283. #endif
  2284. }
  2285. /**
  2286. This function is get called when the module is removed
  2287. @return void
  2288. */
  2289. static void __exit
  2290. adpd142_multisensor_exit(void)
  2291. {
  2292. ADPD142_dbg("%s\n", __func__);
  2293. i2c_del_driver(&adpd142_i2c_driver);
  2294. if (i2c_client)
  2295. i2c_unregister_device(i2c_client);
  2296. }
  2297. module_init(adpd142_multisensor_init);
  2298. module_exit(adpd142_multisensor_exit);
  2299. MODULE_DESCRIPTION();
  2300. MODULE_LICENSE("GPL");
  2301. MODULE_AUTHOR("");