adpd142.c 69 KB

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