ad7146.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. /**
  2. *\mainpage
  3. * AD7146 MLD Driver
  4. \n
  5. * @copyright 2013 Analog Devices Inc.
  6. \n
  7. * Licensed under the GPL Version 3 or later.
  8. * \date April-2013
  9. * \version Driver 1.3
  10. * \version Android ICS 4.0
  11. * \version Linux 3.0.15
  12. */
  13. /**
  14. * \file ad7146.c
  15. * This file is the core driver part of AD7146 for Event interface
  16. * It also has routines for interrupt handling,suspend, resume,
  17. * initialization routines etc.
  18. * AD7146 MLD Driver
  19. * Copyright 2013 Analog Devices Inc.
  20. * Licensed under the GPL Version 3 or later.
  21. */
  22. #include <linux/device.h>
  23. #include <linux/init.h>
  24. #include <linux/input.h>
  25. #include <linux/kernel.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/wait.h>
  28. #include <linux/err.h>
  29. #include <linux/gpio.h>
  30. #include <linux/delay.h>
  31. #include <linux/slab.h>
  32. #include <linux/i2c.h>
  33. #include <linux/sched.h>
  34. #include <linux/types.h>
  35. #include <linux/pm.h>
  36. #include <linux/module.h>
  37. #include <linux/wakelock.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/system.h>
  40. #include <linux/of_gpio.h>
  41. #include "ad7146.h"
  42. #include "ssp.h"
  43. //#include "sensors_core.h"
  44. #define VENDOR_NAME "ADI"
  45. #define DEVICE_NAME "AD7146"
  46. //#define AD7146_DEBUG
  47. #define AD7146_DEBUG_L2
  48. #define OFFSET_FILE_PATH "/efs/grip_cal_data"
  49. #define FULL_POWER_MASK (0xFFFC)
  50. #define FIXED_TH 7000
  51. #define CAL_HIGH_MAX 62000
  52. #define CAL_HIGH_MIN 34250
  53. #define CENTER_CDC 30000
  54. #define CAL_LOW_MAX 29750
  55. #define CAL_LOW_MIN 2000
  56. #define LSB_FOR_CAP 5200
  57. #define MAX_OFFSET 126
  58. #define CAL_RET_SUCCESS (2)
  59. #define CAL_RET_EXIST (1)
  60. #define CAL_RET_NONE (0)
  61. #define CAL_RET_FAIL (3)
  62. #define DO_CALIBRATE 1
  63. #define DONE_CALIBRATE 0
  64. #define LD_POS_AFE_OFFSET(x) ((x & 0x003F) * ((x & 0x0080) ? -1 : 1))
  65. #define LD_NEG_AFE_OFFSET(x) (((x & 0x3F00) >> 8) * ((x & 0x8000) ? 1 : -1))
  66. #define ST_POS_AFE_OFFSET(x) (((x > 63) ? ((x - 63) << 8) | 0x003F : x) | 0x8000)
  67. #define ST_NEG_AFE_OFFSET(x) (((-x > 63) ? (-x - 63) | 0x3F00 : (-x) << 8) | 0x0080)
  68. #ifdef AD7146_DEBUG
  69. #define AD7146_DRIVER_DBG(format, arg...) pr_info("[Grip]:"format,\
  70. ## arg)
  71. #else
  72. #define AD7146_DRIVER_DBG(format, arg...) if (0)
  73. #endif
  74. #ifdef AD7146_DEBUG_L2
  75. #define AD7146_DRIVER_DBG_L2(format, arg...) pr_info("[Grip]:"format,\
  76. ## arg)
  77. #else
  78. #define AD7146_DRIVER_DBG_L2(format, arg...) if (0)
  79. #endif
  80. static struct ad7146_platform_data ad7146_i2c_platform_data = {
  81. // For init touch mode
  82. .regs = {
  83. //Samsung GT - Latest Implementation 1.3 version - Initialization settings, 0x(address)(value)
  84. 0x0080FFFF, 0x00811FEF, 0x00823600, 0x00832626,
  85. 0x00840258, 0x00850258, 0x0086013A, 0x0087013A,
  86. 0x0000C000, 0x00010000, 0x0002004B, 0x000301FF,
  87. 0x0004FFFF, 0x0000C002, 0x00450D01, 0x00050000,
  88. 0x00060000, 0x00070000,
  89. },
  90. // Normal touch mode
  91. .normal_regs = {
  92. //Samsung GT - Latest Implementation 1.3 version - Normal State settings
  93. 0x0000C002, 0x00010001,
  94. },
  95. .fixed_th_full = 40000, // 0 ~ 65535
  96. };
  97. /**
  98. Look up table for the Sensitivity values of the AD7146
  99. */
  100. static int SS_LUT[16] = {
  101. 2500, 2973, 3440, 3908,
  102. 4379, 4847, 5315, 5783,
  103. 6251, 6722, 7190, 7658,
  104. 8128, 8596, 9064, 9532
  105. };
  106. /**
  107. * The global mutex for the locking of the ISR.
  108. */
  109. DEFINE_MUTEX(interrupt_thread_mutex);
  110. /*
  111. This elaborates the sysfs attributes used in the driver
  112. */
  113. /*--------------------------------------------------------------*/
  114. static ssize_t show_dumpregs(struct device *dev,
  115. struct device_attribute *attr, char *buf);
  116. static struct device_attribute dev_attr_sensor_dump =
  117. __ATTR(status, S_IRUSR | S_IRGRP,
  118. show_dumpregs, NULL);
  119. /*--------------------------------------------------------------*/
  120. static ssize_t store_enable(struct device *dev,
  121. struct device_attribute *attr,
  122. const char *buf, size_t count);
  123. static ssize_t show_enable(struct device *dev,
  124. struct device_attribute *attr, char *buf);
  125. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
  126. show_enable, store_enable);
  127. /*--------------------------------------------------------------*/
  128. /**
  129. SYSFS attributes list
  130. */
  131. static struct attribute *ad7146_sysfs_entries[] = {
  132. &dev_attr_enable.attr,
  133. NULL
  134. };
  135. /**
  136. SYSFS attribute Group
  137. */
  138. static struct attribute_group ad7146_attr_group = {
  139. .name = NULL,
  140. .attrs = ad7146_sysfs_entries,
  141. };
  142. /**
  143. Writes to the Device register through i2C.
  144. Used to Write the data to the I2C client's Register through the i2c protocol
  145. @param data The data to be written
  146. @param reg The register address
  147. @param dev The Device Structure
  148. @return 0 on success
  149. @see ad7146_i2c_read
  150. */
  151. static int ad7146_i2c_write(struct device *dev, unsigned short reg,
  152. unsigned short data)
  153. {
  154. struct i2c_client *client = to_i2c_client(dev);
  155. int ret;
  156. u8 *_reg = (u8 *)&reg;
  157. u8 *_data = (u8 *)&data;
  158. u8 tx[4] = {
  159. _reg[1],
  160. _reg[0],
  161. _data[1],
  162. _data[0]
  163. };
  164. ret = i2c_master_send(client, tx, 4);
  165. if (ret < 0)
  166. dev_err(&client->dev, "I2C write error (%d)\n", ret);
  167. return ret;
  168. }
  169. /**
  170. Reads data from the Device register through i2C.
  171. This is used to read the data from the AD7146 I2C client
  172. @param dev The Device Structure (Standard linux call)
  173. @param reg The register address to be read.
  174. @param data The data Read from the Given address.
  175. @return The number of bytes transfered as an integer
  176. @see ad7146_i2c_write
  177. */
  178. static int ad7146_i2c_read(struct device *dev, unsigned short reg,
  179. unsigned short *data)
  180. {
  181. struct i2c_client *client = to_i2c_client(dev);
  182. int ret;
  183. u8 *_reg = (u8 *)&reg;
  184. u8 *_data = (u8 *)data;
  185. u8 tx[2] = {
  186. _reg[1],
  187. _reg[0]
  188. };
  189. u8 rx[2];
  190. ret = i2c_master_send(client, tx, 2);
  191. if (ret >= 0)
  192. ret = i2c_master_recv(client, rx, 2);
  193. if (unlikely(ret < 0)) {
  194. dev_err(&client->dev, "I2C read error (%d)\n", ret);
  195. } else {
  196. _data[0] = rx[1];
  197. _data[1] = rx[0];
  198. }
  199. return ret;
  200. }
  201. static int sensorshutdownmode(struct ad7146_chip *ad7146)
  202. {
  203. unsigned short data = 0;
  204. mutex_lock(&interrupt_thread_mutex);
  205. ad7146->read(ad7146->dev, AD7146_PWR_CTRL, &data);
  206. data = (data | 0x03);
  207. ad7146->write(ad7146->dev, AD7146_PWR_CTRL, data);
  208. mutex_unlock(&interrupt_thread_mutex);
  209. return 0;
  210. }
  211. static void ad7146_force_cal(struct ad7146_chip *ad7146, int cal_time)
  212. {
  213. unsigned short data;
  214. ad7146->write(ad7146->dev, STG_LOW_INT_EN_REG, DISABLE_INT);
  215. ad7146->write(ad7146->dev, STG_HIGH_INT_EN_REG, DISABLE_INT);
  216. ad7146->read(ad7146->dev, AD7146_AMB_COMP_CTRL0_REG, &data);
  217. data = data | AD7146_FORCED_CAL_MASK;
  218. ad7146->write(ad7146->dev, AD7146_AMB_COMP_CTRL0_REG, data);
  219. msleep(cal_time);
  220. ad7146->read(ad7146->dev, CDC_RESULT_S0_REG, &data);
  221. ad7146->write(ad7146->dev, STG_HIGH_INT_EN_REG, ENABLE_STG0);
  222. ad7146->write(ad7146->dev, STG_LOW_INT_EN_REG, ENABLE_STG0);
  223. }
  224. /**
  225. This function is used to indicate Full Grip state by event
  226. @param ad7146 The AD7146 chip structure pointer
  227. @return void
  228. */
  229. static inline void indicatefullgripstate(struct ad7146_chip *ad7146)
  230. {
  231. AD7146_DRIVER_DBG("indicatefullgripstate()\n");
  232. if ((ad7146->eventcheck == 1) && (ad7146->onoff_flags == 1)) {
  233. input_report_rel(ad7146->input, REL_MISC, EVENT_FULL_GRIP + 1);
  234. input_sync(ad7146->input);
  235. }
  236. }
  237. /**
  238. This function is used to indicate No Grip state by event
  239. @param ad7146 The AD7146 chip structure pointer
  240. @return void
  241. */
  242. static inline void indicatenormalstate(struct ad7146_chip *ad7146)
  243. {
  244. AD7146_DRIVER_DBG("indicatenormalstate()\n");
  245. if ((ad7146->eventcheck == 1) && (ad7146->onoff_flags == 1)) {
  246. input_report_rel(ad7146->input, REL_MISC, EVENT_NO_GRIP + 1);
  247. input_sync(ad7146->input);
  248. }
  249. }
  250. /**
  251. This Function is used to determine the No Grip and
  252. enter to the NORMAL mode of operation
  253. @param ad7146 The Chip Structure.
  254. @return void - Nothing returned
  255. */
  256. static void initnormalgrip(struct ad7146_chip *ad7146)
  257. {
  258. unsigned int lcnt = 0;
  259. indicatenormalstate(ad7146);
  260. ad7146->write(ad7146->dev, STG_LOW_INT_EN_REG,
  261. DISABLE_INT);
  262. ad7146->write(ad7146->dev, STG_HIGH_INT_EN_REG,
  263. DISABLE_INT);
  264. for (lcnt = 0; lcnt < (sizeof(ad7146_i2c_platform_data.normal_regs)/sizeof(int));
  265. lcnt++) {
  266. unsigned short addr;
  267. unsigned short value;
  268. addr = (unsigned short)((ad7146_i2c_platform_data.normal_regs[lcnt] &
  269. 0xffff0000) >> 16);
  270. value = (unsigned short)(ad7146_i2c_platform_data.normal_regs[lcnt] &
  271. 0x0000ffff);
  272. ad7146->write(ad7146->dev, addr, value);
  273. }
  274. ad7146_force_cal(ad7146, SLEEP_TIME_TO_CALI_INT);
  275. ad7146->pw_on_grip_status = DRIVER_STATE_NORMAL;
  276. AD7146_DRIVER_DBG("Forced Calibration in func %s\n", __func__);
  277. ad7146->write(ad7146->dev, STG_LOW_INT_EN_REG, ENABLE_STG0);
  278. ad7146->write(ad7146->dev, STG_HIGH_INT_EN_REG, ENABLE_STG0);
  279. }
  280. /**
  281. This Function is used in the POWER_ON GRIP detection to
  282. determine the Full Grip status.
  283. @param ad7146 The Chip Structure.
  284. @return void - Nothing returned
  285. */
  286. static void initfullgrip(struct ad7146_chip *ad7146)
  287. {
  288. unsigned short sf_amb = 0;
  289. unsigned short cdc_data = 0;
  290. unsigned short theshold_low;
  291. indicatefullgripstate(ad7146);
  292. if (ad7146->cal_flags == CAL_RET_NONE)
  293. theshold_low = ad7146_i2c_platform_data.fixed_th_full;
  294. else
  295. theshold_low = ad7146_i2c_platform_data.cal_fixed_th_full;
  296. ad7146->write(ad7146->dev, AD7146_STG_CAL_EN_REG, ENABLE_STG0);
  297. ad7146->write(ad7146->dev, STG_0_LOW_THRESHOLD, theshold_low);
  298. /* Change the Ambient to set the theshold as a fixed value
  299. DONOT CHANGE THE FOLLOWING LINE*/
  300. ad7146->write(ad7146->dev, DRIVER_STG0_SF_AMBIENT , FULL_SCALE_VALUE);
  301. ad7146->pw_on_grip_status = DRIVER_STATE_FULL_GRIP;
  302. ad7146->read(ad7146->dev, CDC_RESULT_S0_REG, &cdc_data);
  303. ad7146->read(ad7146->dev, STG_0_LOW_THRESHOLD, &theshold_low);
  304. ad7146->read(ad7146->dev, DRIVER_STG0_SF_AMBIENT , &sf_amb);
  305. AD7146_DRIVER_DBG_L2(" %s, AMB %d CurrentCDC %d theshold_low %d\n", __func__,
  306. sf_amb, cdc_data, theshold_low);
  307. ad7146->write(ad7146->dev, STG_LOW_INT_EN_REG, ENABLE_STG0);
  308. }
  309. /**
  310. This function is used to identify and report the first grip status.
  311. @param ad7146 The AD7146 chip structure pointer
  312. @return void - Nothing Returned
  313. */
  314. static int sensorgrip(struct ad7146_chip *ad7146)
  315. {
  316. unsigned short conv0_avg = 0;
  317. unsigned short threshold;
  318. mutex_lock(&interrupt_thread_mutex);
  319. ad7146->read(ad7146->dev, CDC_RESULT_S0_REG, &conv0_avg);
  320. AD7146_DRIVER_DBG("conv0_avg = %5d\n", conv0_avg);
  321. if (conv0_avg < OVER_FLOW_SCALE_VALUE)
  322. conv0_avg = FULL_SCALE_VALUE;
  323. if (ad7146->cal_flags == CAL_RET_NONE)
  324. threshold = ad7146_i2c_platform_data.fixed_th_full;
  325. else
  326. threshold = ad7146_i2c_platform_data.cal_fixed_th_full;
  327. if (conv0_avg < threshold) {
  328. initnormalgrip(ad7146);
  329. } else {
  330. initfullgrip(ad7146);
  331. }
  332. mutex_unlock(&interrupt_thread_mutex) ;
  333. return 0;
  334. }
  335. /**
  336. This is to configure the device with the register set defined in platform file.
  337. Finally calibration is done and status registers will be cleared.
  338. * @param ad7146 The Device structure
  339. * @return void Nothing Returned
  340. */
  341. static int ad7146_hw_init(struct ad7146_chip *ad7146)
  342. {
  343. int lcnt = 0;
  344. mutex_lock(&interrupt_thread_mutex) ;
  345. /** configuration CDC and interrupts */
  346. for (lcnt = 0; lcnt < (sizeof(ad7146_i2c_platform_data.regs)/sizeof(int)); lcnt++) {
  347. unsigned short addr;
  348. unsigned short value;
  349. addr = (unsigned short)((ad7146_i2c_platform_data.regs[lcnt] &
  350. 0xffff0000) >> 16);
  351. value = (unsigned short)(ad7146_i2c_platform_data.regs[lcnt] &
  352. 0x0000ffff);
  353. if (addr == DRIVER_STG0_AFEOFFSET) {
  354. if (ad7146->cal_flags != CAL_RET_NONE)
  355. value = ad7146_i2c_platform_data.cal_offset;
  356. AD7146_DRIVER_DBG_L2(" %s, Addr %x Val %x\n", __func__, addr, value);
  357. }
  358. if (addr == AD7146_AMB_COMP_CTRL0_REG)
  359. value = value & 0xBFFF;
  360. ad7146->write(ad7146->dev, addr, value);
  361. }
  362. msleep(SLEEP_TIME_TO_CALI_INT);
  363. ad7146->prev_state_value = -1;
  364. ad7146->state_value = EVENT_NO_GRIP;
  365. ad7146->prevhigh = 0;
  366. AD7146_DRIVER_DBG("Force calibration done\n");
  367. mutex_unlock(&interrupt_thread_mutex);
  368. return 0;
  369. }
  370. /**
  371. This Function is used for the creating a Register Dump of the registers of the AD7146.
  372. @param dev The Device Id and Information structure(Linux Standard argument)
  373. @param attr standard Linux Device attributes to the AD7146
  374. @param buf The buffer to store the data to be written
  375. @param count The count of bytes to be transfered to the Device
  376. \note This is evoked upon an echo request in /sys/../<Device> region.
  377. \note This also prints the results in the console for the user.
  378. @return count of data written
  379. */
  380. static ssize_t show_dumpregs(struct device *dev,
  381. struct device_attribute *attr, char *buf)
  382. {
  383. struct ad7146_chip *ad7146 = dev_get_drvdata(dev);
  384. unsigned short u16temp;
  385. unsigned int u32_lpcnt = 0;
  386. mutex_lock(&interrupt_thread_mutex);
  387. pr_info("[Grip]: Bank 1 register\n");
  388. for (u32_lpcnt = 0; u32_lpcnt < 0x16; u32_lpcnt++) {
  389. ad7146->read(ad7146->dev, (unsigned short)u32_lpcnt, &u16temp);
  390. pr_info("[Grip]: Reg 0X%x val 0x%x\n",
  391. u32_lpcnt, u16temp);
  392. }
  393. ad7146->read(ad7146->dev, (unsigned short)0x042, &u16temp);
  394. pr_info("[Grip]: Reg 0X0042 val 0x%x\n", u16temp);
  395. ad7146->read(ad7146->dev, (unsigned short)0x0045, &u16temp);
  396. pr_info("[Grip]: Reg 0X0045 val 0x%x\n", u16temp);
  397. pr_info("[Grip]: Bank 2 register - Config\n");
  398. for (u32_lpcnt = 0x080; u32_lpcnt < 0x090; u32_lpcnt++) {
  399. ad7146->read(ad7146->dev, (unsigned short)u32_lpcnt, &u16temp);
  400. pr_info("[Grip]: Reg 0X%x val 0x%x\n",
  401. u32_lpcnt, u16temp);
  402. }
  403. pr_info("[Grip]: Bank 3 register - Results\n");
  404. for (u32_lpcnt = 0x0E0; u32_lpcnt < 0x128; u32_lpcnt++) {
  405. ad7146->read(ad7146->dev, (unsigned short)u32_lpcnt, &u16temp);
  406. pr_info("[Grip]: Reg 0X%x val 0x%x\n",
  407. u32_lpcnt, u16temp);
  408. }
  409. mutex_unlock(&interrupt_thread_mutex);
  410. return sprintf(buf, "0\n");
  411. }
  412. static int ad7146_open_calibration(struct ad7146_chip *ad7146)
  413. {
  414. struct file *offset_filp = NULL;
  415. unsigned short st_file[2] = {0};
  416. int err = 0;
  417. mm_segment_t old_fs;
  418. old_fs = get_fs();
  419. set_fs(KERNEL_DS);
  420. offset_filp = filp_open(OFFSET_FILE_PATH, O_CREAT | O_RDONLY | O_SYNC, 0666);
  421. if (IS_ERR(offset_filp)) {
  422. pr_err("%s: no offset file\n", __func__);
  423. err = PTR_ERR(offset_filp);
  424. if (err != -ENOENT)
  425. pr_err("%s: Can't open cancelation file\n", __func__);
  426. set_fs(old_fs);
  427. return err;
  428. }
  429. err = offset_filp->f_op->read(offset_filp,
  430. (char *)&st_file, sizeof(unsigned short) * 2, &offset_filp->f_pos);
  431. if (err != sizeof(unsigned short) * 2) {
  432. pr_err("%s: Can't write the offset data to file\n", __func__);
  433. err = -EIO;
  434. }
  435. if ((st_file[0] != 0) && (st_file[1] != 0)) {
  436. ad7146->cal_flags = CAL_RET_EXIST;
  437. ad7146_i2c_platform_data.cal_offset = st_file[0];
  438. ad7146_i2c_platform_data.cal_fixed_th_full = st_file[1];
  439. } else {
  440. ad7146->cal_flags = CAL_RET_NONE;
  441. ad7146_i2c_platform_data.cal_offset = 0;
  442. ad7146_i2c_platform_data.cal_fixed_th_full = 0;
  443. }
  444. pr_info("[Grip]: %s, %d, %4x,%d\n", __func__, ad7146->cal_flags, st_file[0], st_file[1]);
  445. filp_close(offset_filp, current->files);
  446. set_fs(old_fs);
  447. return err;
  448. }
  449. /**
  450. This Function is used to enable or to disable the device the Sysfs attribute is
  451. given as "enable" writing a '0' Disables the device.
  452. While writing a '1' enables the device.
  453. @param dev The Device Id and Information structure(Linux Standard argument)
  454. @param attr standard Linux Device attributes to the AD7146.
  455. @param buf The buffer to store the data to be written.
  456. @param count The count of bytes to be transfered to the Device.
  457. \note This is evoked upon an echo request in /sys/../<Device> region.
  458. \note This also prints the results in the console for the user.
  459. @return count of data written.
  460. */
  461. static ssize_t store_enable(struct device *dev,
  462. struct device_attribute *attr,
  463. const char *buf, size_t count)
  464. {
  465. struct ad7146_chip *ad7146 = dev_get_drvdata(dev);
  466. int val, err;
  467. err = kstrtoint(buf, 10, &val);
  468. if (err < 0) {
  469. pr_err("[Grip]: %s, kstrtoint failed\n", __func__);
  470. } else {
  471. pr_info("[Grip]: %s: enable %d val %d\n", __func__, ad7146->eventcheck, val);
  472. if ((val == 1) || (val == 0)) {
  473. if (ad7146->eventcheck != ((unsigned short) val)) {
  474. ad7146->eventcheck = (unsigned short) val;
  475. if (ad7146->eventcheck == 1) {
  476. ad7146_open_calibration(ad7146);
  477. ad7146_hw_init(ad7146);
  478. enable_irq(ad7146->irq);
  479. enable_irq_wake(ad7146->irq);
  480. sensorgrip(ad7146);
  481. } else {
  482. disable_irq_wake(ad7146->irq);
  483. disable_irq(ad7146->irq);
  484. ad7146->write(ad7146->dev, STG_HIGH_INT_EN_REG,
  485. DISABLE_INT);
  486. ad7146->write(ad7146->dev, STG_LOW_INT_EN_REG,
  487. DISABLE_INT);
  488. sensorshutdownmode(ad7146);
  489. }
  490. }
  491. }
  492. }
  493. return count;
  494. }
  495. /**
  496. This Function is used to show the enabled status of the device.
  497. Status '1' signifies the device is ENABLED,
  498. while the status '0' signifies a DISABLED device.
  499. @param dev The Device Id and Information structure(Linux Standard argument)
  500. @param attr standard Linux Device attributes to the AD7146.
  501. @param buf The buffer to store the data to be written.
  502. \note This is evoked upon an cat request in /sys/../<Device> region.
  503. \note This also prints the results in the console for the user.
  504. @return The count of data written.
  505. */
  506. static ssize_t show_enable(struct device *dev,
  507. struct device_attribute *attr, char *buf)
  508. {
  509. struct ad7146_chip *ad7146 = dev_get_drvdata(dev);
  510. return sprintf(buf, "%d\n", ad7146->eventcheck);
  511. }
  512. /**
  513. This callback_tmrfn is the callback function from MLD state machine
  514. for the event intimation of the AD7146 device.
  515. Here the current & previous grip state are compared & the event is sent
  516. accordingly.
  517. @param data unsigned long data
  518. @return 0 on success
  519. */
  520. static inline void callback_tmrfn(unsigned long data)
  521. {
  522. struct ad7146_chip *ad7146 = (struct ad7146_chip *)data;
  523. short state_value = ad7146->state_value;
  524. short prev_state_value = ad7146->prev_state_value;
  525. if (state_value != prev_state_value) {
  526. ad7146->prev_state_value = ad7146->state_value;
  527. if ((ad7146->eventcheck == 1) && (ad7146->onoff_flags == 1)) {
  528. input_report_rel(ad7146->input, REL_MISC,
  529. (unsigned int)state_value + 1);
  530. input_sync(ad7146->input);
  531. pr_info("[Grip]: %s: state_value = %d\n", __func__, state_value);
  532. } else if ((ad7146->eventcheck == 1) && (ad7146->onoff_flags == 0)) {
  533. pr_info("[Grip]: %s, event locked by flags, %d\n", __func__, ad7146->onoff_flags);
  534. }
  535. }
  536. }
  537. /**
  538. This MLD State Machine is used to determine the current grip state
  539. @param ad7146 The AD7146 chip structure
  540. @return 0 on success
  541. */
  542. static int sldstateMachine(struct ad7146_chip *ad7146)
  543. {
  544. unsigned short high = (ad7146->high_status & 3);
  545. unsigned short prevhigh = (ad7146->prevhigh & 3);
  546. short state_value = ad7146->state_value;
  547. if ((high == 0) && (prevhigh == 0)) {
  548. state_value = EVENT_NO_GRIP;
  549. high = 0;
  550. ad7146->high_status = high;
  551. ad7146->state_value = state_value;
  552. return 0;
  553. }
  554. if ((high == 1) && (prevhigh == 0)) {
  555. AD7146_DRIVER_DBG("State = 1e\n");
  556. state_value = EVENT_FULL_GRIP;
  557. }
  558. if ((high == 0) && (prevhigh == 1)) {
  559. AD7146_DRIVER_DBG("State = 0x\n");
  560. state_value = EVENT_NO_GRIP;
  561. }
  562. prevhigh = high;
  563. ad7146->high_status = high;
  564. ad7146->prevhigh = prevhigh;
  565. ad7146->state_value = state_value;
  566. callback_tmrfn((unsigned long)ad7146);
  567. return 0;
  568. }
  569. /**
  570. * \fn static int ad7146_hw_detect(struct ad7146_chip *ad7146)
  571. * This Routine reads the Device ID to confirm the existance
  572. * of the Device in the System.
  573. @param ad7146 The Device structure
  574. @return 0 on Successful detection of the device,-ENODEV on err.
  575. */
  576. static int ad7146_hw_detect(struct ad7146_chip *ad7146)
  577. {
  578. unsigned short data;
  579. ad7146->read(ad7146->dev, AD7146_PARTID_REG, &data);
  580. switch (data & 0xFFF0) {
  581. case AD7146_PARTID:
  582. ad7146->product = AD7146_PRODUCT_ID;
  583. ad7146->version = data & 0xF;
  584. dev_info(ad7146->dev, "[Grip]: found AD7146 , rev:%d\n",
  585. ad7146->version);
  586. return 0;
  587. default:
  588. dev_err(ad7146->dev,
  589. "[Grip]: ad7146 Not Found,ID %04x\n", data);
  590. return -ENODEV;
  591. }
  592. }
  593. /**
  594. This function is used check the stage & accordingly calculate the
  595. hysteresis compensation required.
  596. @param ad7146 The AD7146 chip structure pointer
  597. @return void - Nothing returned
  598. */
  599. static void ad7146_hysteresis_comp(struct ad7146_chip *ad7146)
  600. {
  601. unsigned short u16_high_threshold = 0;
  602. unsigned short u16_sf_ambient = 0;
  603. unsigned int result = 0;
  604. if (ad7146->high_status > (short)ad7146->prevhigh) {
  605. if ((ad7146->high_status & 1) && (ad7146->state_value != EVENT_FULL_GRIP)) {
  606. ad7146->read(ad7146->dev, DRIVER_STG0_HIGH_THRESHOLD,
  607. &u16_high_threshold);
  608. ad7146->read(ad7146->dev, DRIVER_STG0_SF_AMBIENT,
  609. &u16_sf_ambient);
  610. result = HYS(u16_sf_ambient, u16_high_threshold);
  611. ad7146->write(ad7146->dev, DRIVER_STG0_HIGH_THRESHOLD,
  612. (unsigned short)result);
  613. AD7146_DRIVER_DBG_L2(" %s, N STG0 HT 0x%d->0x%d\n", __func__,
  614. u16_high_threshold, result);
  615. }
  616. } else if (ad7146->high_status < ad7146->prevhigh) {
  617. if ((!(ad7146->high_status & 1)) &&
  618. (ad7146->prevhigh & 1)) {
  619. ad7146->read(ad7146->dev, DRIVER_STG0_HIGH_THRESHOLD,
  620. &u16_high_threshold);
  621. ad7146->read(ad7146->dev, DRIVER_STG0_SF_AMBIENT,
  622. &u16_sf_ambient);
  623. result = HYS_POS(u16_sf_ambient, u16_high_threshold);
  624. ad7146->write(ad7146->dev, DRIVER_STG0_HIGH_THRESHOLD,
  625. (unsigned short)result);
  626. AD7146_DRIVER_DBG_L2(" %s, P STG0 HT 0x%d->0x%d\n", __func__,
  627. u16_high_threshold, result);
  628. }
  629. }
  630. }
  631. /**
  632. IRQ Handler -- Handles the Grip & Normal mode of proximity detection
  633. @param handle The data of the AD7146 Device
  634. @param irq The Interrupt Request queue to be assigned for the device.
  635. @return IRQ_HANDLED
  636. */
  637. static irqreturn_t ad7146_isr(int irq, void *handle)
  638. {
  639. struct ad7146_chip *ad7146 = handle;
  640. mutex_lock(&interrupt_thread_mutex) ;
  641. wake_lock_timeout(&ad7146->grip_wake_lock, 3 * HZ);
  642. if (!work_pending(&ad7146->work)) {
  643. schedule_work(&ad7146->work);
  644. } else {
  645. /*Cleared the interrupt for future intterupts to occur*/
  646. ad7146->read(ad7146->dev, STG_LOW_INT_STA_REG,
  647. &ad7146->low_status);
  648. ad7146->read(ad7146->dev, STG_HIGH_INT_STA_REG,
  649. &ad7146->high_status);
  650. }
  651. mutex_unlock(&interrupt_thread_mutex);
  652. return IRQ_HANDLED;
  653. }
  654. /**
  655. Interrupt work Handler -- Handles the Grip & Normal mode of proximity
  656. detection from the ISR
  657. @param work The work structure for the AD7146 chip
  658. @return void Nothing returned
  659. */
  660. static void ad7146_interrupt_thread(struct work_struct *work)
  661. {
  662. struct ad7146_chip *ad7146 = container_of(work,
  663. struct ad7146_chip, work);
  664. unsigned short data = 0;
  665. unsigned short threshold;
  666. mutex_lock(&interrupt_thread_mutex);
  667. ad7146->read(ad7146->dev, STG_HIGH_INT_STA_REG,
  668. &ad7146->high_status);
  669. ad7146->read(ad7146->dev, STG_LOW_INT_STA_REG,
  670. &ad7146->low_status);
  671. AD7146_DRIVER_DBG_L2(" %s, HS%x LS%x\n", __func__,
  672. ad7146->high_status, ad7146->low_status);
  673. if (ad7146->cal_flags == CAL_RET_NONE)
  674. threshold = ad7146_i2c_platform_data.fixed_th_full;
  675. else
  676. threshold = ad7146_i2c_platform_data.cal_fixed_th_full;
  677. ad7146->read(ad7146->dev, CDC_RESULT_S0_REG, &data);
  678. if (data < OVER_FLOW_SCALE_VALUE)
  679. data = FULL_SCALE_VALUE;
  680. pr_info("[Grip]: %s, data(%d)\n", __func__, data);
  681. if (ad7146->pw_on_grip_status == DRIVER_STATE_FULL_GRIP) { // init touch mode
  682. pr_info("[Grip]: %s, FULL_GRIP\n", __func__);
  683. if ((ad7146->low_status & 1) == 1) {
  684. if ((OVER_FLOW_SCALE_VALUE < data) &&
  685. (data <= (threshold))) {
  686. initnormalgrip(ad7146);
  687. } else {
  688. ad7146->write(ad7146->dev, DRIVER_STG0_SF_AMBIENT , FULL_SCALE_VALUE);
  689. ad7146->write(ad7146->dev, STG_0_LOW_THRESHOLD, threshold);
  690. pr_info("[Grip]: %s, reset Full Grip\n", __func__);
  691. }
  692. } else {
  693. if (data < threshold)
  694. initnormalgrip(ad7146);
  695. }
  696. } else { // normal mode
  697. pr_info("[Grip]: %s, NORMAL\n", __func__);
  698. if (ad7146->low_status != 0) {
  699. if (OVER_FLOW_SCALE_VALUE < data && data < FULL_SCALE_VALUE) {
  700. ad7146_force_cal(ad7146, SLEEP_TIME_TO_CALI_INT);
  701. AD7146_DRIVER_DBG("FCalib(%d) in Low INT\n", data);
  702. }
  703. } else {
  704. ad7146_hysteresis_comp(ad7146);
  705. sldstateMachine(ad7146);
  706. }
  707. }
  708. mutex_unlock(&interrupt_thread_mutex);
  709. }
  710. static int offset_write(struct ad7146_chip *ad7146, unsigned short data)
  711. {
  712. ad7146->write(ad7146->dev, DRIVER_STG0_AFEOFFSET, data);
  713. msleep(SLEEP_TIME_TO_CALI_INT);
  714. ad7146->read(ad7146->dev, CDC_RESULT_S0_REG, &data);
  715. ad7146->read(ad7146->dev, DRIVER_STG0_AFEOFFSET, &data);
  716. return 0;
  717. }
  718. static int ad7146_offset_check(struct ad7146_chip *ad7146)
  719. {
  720. int err = 0;
  721. short cal_offset;
  722. unsigned short data, offset;
  723. offset = (unsigned short)(ad7146_i2c_platform_data.regs[STAGE_AFE_OFFSET] &
  724. 0x0000ffff);
  725. offset_write(ad7146, offset);
  726. ad7146->read(ad7146->dev, CDC_RESULT_S0_REG, &data);
  727. ad7146->read(ad7146->dev, DRIVER_STG0_AFEOFFSET, &offset);
  728. if (data < CAL_LOW_MIN || CAL_HIGH_MAX < data) {
  729. cal_offset = LD_POS_AFE_OFFSET(offset) + LD_NEG_AFE_OFFSET(offset);
  730. cal_offset += 8;
  731. pr_info("[Grip]: %s,(%d)\n", __func__, cal_offset);
  732. if (0 <= cal_offset && cal_offset <= MAX_OFFSET) {
  733. offset = ST_POS_AFE_OFFSET(cal_offset);
  734. } else if (-MAX_OFFSET <= cal_offset && cal_offset < 0) {
  735. offset = ST_NEG_AFE_OFFSET(cal_offset);
  736. } else {
  737. err = -1;
  738. pr_err("[Grip]: %s, Cal offset ERROR(%d)\n", __func__, cal_offset);
  739. }
  740. offset_write(ad7146, offset);
  741. } else {
  742. pr_info("[Grip]: %s, no change offset(%d)\n", __func__, data);
  743. return 0;
  744. }
  745. ad7146->read(ad7146->dev, CDC_RESULT_S0_REG, &data);
  746. ad7146->read(ad7146->dev, DRIVER_STG0_AFEOFFSET, &offset);
  747. if (data < CAL_LOW_MIN || CAL_HIGH_MAX < data) {
  748. cal_offset = LD_POS_AFE_OFFSET(offset) + LD_NEG_AFE_OFFSET(offset);
  749. if (err < 0)
  750. cal_offset -= 8;
  751. else
  752. cal_offset -= 16;
  753. pr_info("[Grip]: %s,(%d)\n", __func__, cal_offset);
  754. if (0 <= cal_offset && cal_offset <= MAX_OFFSET) {
  755. offset = ST_POS_AFE_OFFSET(cal_offset);
  756. } else if (-MAX_OFFSET <= cal_offset && cal_offset < 0) {
  757. offset = ST_NEG_AFE_OFFSET(cal_offset);
  758. } else {
  759. err = -1;
  760. pr_err("[Grip]: %s, Cal offset ERROR(%d)\n", __func__, cal_offset);
  761. }
  762. offset_write(ad7146, offset);
  763. } else {
  764. pr_info("[Grip]: %s, no change offset(%d)\n", __func__, data);
  765. return 0;
  766. }
  767. return err;
  768. }
  769. static int ad7146_calibration(struct ad7146_chip *ad7146, bool cal_flag)
  770. {
  771. struct file *offset_filp = NULL;
  772. mm_segment_t old_fs;
  773. short cal_offset, count = 4;
  774. unsigned short data, offset, temp_offset, temp_fixed_th;
  775. unsigned short st_file[2] = {0};
  776. int err = 0;
  777. ad7146->write(ad7146->dev, STG_LOW_INT_EN_REG, DISABLE_INT);
  778. ad7146->write(ad7146->dev, STG_HIGH_INT_EN_REG, DISABLE_INT);
  779. ad7146->cal_flags = CAL_RET_FAIL;
  780. temp_offset = ad7146_i2c_platform_data.cal_offset;
  781. temp_fixed_th = ad7146_i2c_platform_data.cal_fixed_th_full;
  782. if (cal_flag) {
  783. ad7146->read(ad7146->dev, AD7146_PWR_CTRL, &data);
  784. data &= FULL_POWER_MASK;
  785. ad7146->write(ad7146->dev, AD7146_PWR_CTRL, data);
  786. msleep(SLEEP_TIME_TO_LOW_POWER + SLEEP_TIME_TO_CALI_INT);
  787. ad7146_offset_check(ad7146);
  788. while (count >= 0) {
  789. ad7146->read(ad7146->dev, CDC_RESULT_S0_REG, &data);
  790. ad7146->read(ad7146->dev, DRIVER_STG0_AFEOFFSET, &offset);
  791. cal_offset = LD_POS_AFE_OFFSET(offset) + LD_NEG_AFE_OFFSET(offset);
  792. pr_info("[Grip]: %s, %d, %x(%d), %d\n", __func__,
  793. data, offset, cal_offset, count);
  794. if ((CAL_LOW_MAX < data && data <= CAL_HIGH_MIN) &&
  795. (-MAX_OFFSET < cal_offset && cal_offset <= MAX_OFFSET)) {
  796. st_file[0] = offset;
  797. st_file[1] = data + FIXED_TH;
  798. ad7146_i2c_platform_data.cal_fixed_th_full = data + FIXED_TH;
  799. ad7146_i2c_platform_data.cal_offset = offset;
  800. ad7146->cal_flags = CAL_RET_SUCCESS;
  801. pr_info("[Grip]: %s, cal_fixed_th_full(%u), cal_offset(%u), cal_flags(%d)\n",
  802. __func__, ad7146_i2c_platform_data.cal_fixed_th_full,
  803. ad7146_i2c_platform_data.cal_offset, ad7146->cal_flags);
  804. break;
  805. } else if (CAL_LOW_MIN < data && data <= CAL_HIGH_MAX) {
  806. if (data > CENTER_CDC)
  807. cal_offset = cal_offset - count;
  808. else
  809. cal_offset = cal_offset + count;
  810. if (0 <= cal_offset && cal_offset <= MAX_OFFSET) {
  811. offset = ST_POS_AFE_OFFSET(cal_offset);
  812. } else if (-MAX_OFFSET <= cal_offset && cal_offset < 0) {
  813. offset = ST_NEG_AFE_OFFSET(cal_offset);
  814. } else {
  815. err = -1;
  816. pr_err("%s, offset ERROR(%d)\n", __func__, data);
  817. break;
  818. }
  819. offset_write(ad7146, offset);
  820. } else {
  821. err = -1;
  822. pr_err("%s, raw data ERROR(%d)\n", __func__, data);
  823. break;
  824. }
  825. if (count > 0)
  826. count /= 2;
  827. else
  828. count -= 1;
  829. }
  830. } else {
  831. st_file[0] = 0;
  832. st_file[1] = 0;
  833. ad7146->cal_flags = CAL_RET_NONE;
  834. ad7146_i2c_platform_data.cal_fixed_th_full = 0;
  835. ad7146_i2c_platform_data.cal_offset = 0;
  836. offset = (unsigned short)(ad7146_i2c_platform_data.regs[STAGE_AFE_OFFSET] & 0x0000ffff);
  837. }
  838. if (ad7146->cal_flags == CAL_RET_FAIL) {
  839. pr_err("[Grip]: %s, cal fail\n", __func__);
  840. if (ad7146_i2c_platform_data.cal_fixed_th_full != 0) {
  841. offset = ad7146_i2c_platform_data.cal_offset;
  842. ad7146->cal_flags = CAL_RET_EXIST;
  843. } else {
  844. offset = (unsigned short)(ad7146_i2c_platform_data.regs[STAGE_AFE_OFFSET] & 0x0000ffff);
  845. ad7146->cal_flags = CAL_RET_NONE;
  846. }
  847. } else {
  848. pr_info("[Grip]: %s, cal success\n", __func__);
  849. }
  850. offset_write(ad7146, offset);
  851. old_fs = get_fs();
  852. set_fs(KERNEL_DS);
  853. offset_filp = filp_open(OFFSET_FILE_PATH,
  854. O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, 0666);
  855. if (IS_ERR(offset_filp)) {
  856. pr_err("[Grip]: %s: Can't open file\n", __func__);
  857. set_fs(old_fs);
  858. err = PTR_ERR(offset_filp);
  859. goto save_fail;
  860. }
  861. err = offset_filp->f_op->write(offset_filp,
  862. (char *)&st_file, sizeof(unsigned short) * 2, &offset_filp->f_pos);
  863. if (err != sizeof(unsigned short) * 2) {
  864. pr_err("[Grip]: %s: Can't write the offset data to file\n", __func__);
  865. err = -EIO;
  866. filp_close(offset_filp, current->files);
  867. set_fs(old_fs);
  868. goto save_fail;
  869. }
  870. filp_close(offset_filp, current->files);
  871. set_fs(old_fs);
  872. return err;
  873. save_fail:
  874. if (cal_flag) {
  875. if (ad7146->cal_flags == CAL_RET_SUCCESS) {
  876. ad7146_i2c_platform_data.cal_fixed_th_full = temp_fixed_th;
  877. ad7146_i2c_platform_data.cal_offset = temp_offset;
  878. if (temp_fixed_th == 0) {
  879. offset = (unsigned short)(ad7146_i2c_platform_data.regs[STAGE_AFE_OFFSET] & 0x0000ffff);
  880. ad7146->cal_flags = CAL_RET_NONE;
  881. } else {
  882. offset = temp_offset;
  883. ad7146->cal_flags = CAL_RET_EXIST;
  884. }
  885. }
  886. } else {
  887. ad7146_i2c_platform_data.cal_fixed_th_full = temp_fixed_th;
  888. ad7146_i2c_platform_data.cal_offset = temp_offset;
  889. if (temp_fixed_th == 0) {
  890. offset = (unsigned short)(ad7146_i2c_platform_data.regs[STAGE_AFE_OFFSET] & 0x0000ffff);
  891. ad7146->cal_flags = CAL_RET_NONE;
  892. } else {
  893. offset = temp_offset;
  894. ad7146->cal_flags = CAL_RET_EXIST;
  895. }
  896. }
  897. offset_write(ad7146, offset);
  898. return err;
  899. }
  900. static ssize_t ad7146_name_show(struct device *dev,
  901. struct device_attribute *attr, char *buf)
  902. {
  903. return snprintf(buf, PAGE_SIZE, "%s\n", DEVICE_NAME);
  904. }
  905. static ssize_t ad7146_vendor_show(struct device *dev,
  906. struct device_attribute *attr, char *buf)
  907. {
  908. return snprintf(buf, PAGE_SIZE, "%s\n", VENDOR_NAME);
  909. }
  910. static ssize_t ad7146_raw_data_show(struct device *dev,
  911. struct device_attribute *attr, char *buf)
  912. {
  913. struct ad7146_chip *ad7146 = dev_get_drvdata(dev);
  914. unsigned short u16temp;
  915. if (ad7146->during_cal_flags == DONE_CALIBRATE) {
  916. ad7146->read(ad7146->dev, CDC_RESULT_S0_REG, &u16temp);
  917. if (u16temp < OVER_FLOW_SCALE_VALUE)
  918. u16temp = 0xFFFF;
  919. ad7146->during_cal_data = u16temp;
  920. pr_info("[Grip]: %s, raw_data : %d\n", __func__, u16temp);
  921. }
  922. return snprintf(buf, PAGE_SIZE, "%d\n", ad7146->during_cal_data);
  923. }
  924. static ssize_t ad7146_onoff_show(struct device *dev,
  925. struct device_attribute *attr, char *buf)
  926. {
  927. struct ad7146_chip *ad7146 = dev_get_drvdata(dev);
  928. return snprintf(buf, PAGE_SIZE, "%d\n", ad7146->onoff_flags);
  929. }
  930. static ssize_t ad7146_onoff_store(struct device *dev,
  931. struct device_attribute *attr, const char *buf, size_t size)
  932. {
  933. struct ad7146_chip *ad7146 = dev_get_drvdata(dev);
  934. int val, err;
  935. err = kstrtoint(buf, 10, &val);
  936. if (err < 0) {
  937. pr_err("[Grip]: %s, kstrtoint failed\n", __func__);
  938. } else {
  939. pr_info("[Grip]: %s: enable %d val %d\n", __func__, ad7146->onoff_flags, val);
  940. if ((val == 1) || (val == 0)) {
  941. if (ad7146->onoff_flags != ((unsigned char) val)) {
  942. ad7146->onoff_flags = (unsigned char) val;
  943. if ((ad7146->eventcheck == 1) && (ad7146->onoff_flags == 0)) {
  944. input_report_rel(ad7146->input, REL_MISC,
  945. EVENT_NO_GRIP + 1);
  946. input_sync(ad7146->input);
  947. } else if ((ad7146->eventcheck == 1) && (ad7146->onoff_flags == 1)) {
  948. input_report_rel(ad7146->input, REL_MISC,
  949. ad7146->state_value + 1);
  950. input_sync(ad7146->input);
  951. pr_info("[Grip]: %s : report state %d\n", __func__, ad7146->state_value);
  952. }
  953. }
  954. }
  955. }
  956. return size;
  957. }
  958. static ssize_t ad7146_threshold_show(struct device *dev,
  959. struct device_attribute *attr, char *buf)
  960. {
  961. struct ad7146_chip *ad7146 = dev_get_drvdata(dev);
  962. unsigned int fixed_threshold, threshold;
  963. unsigned int sensitivity, data;
  964. unsigned short stg_th, stg_amb;
  965. if (ad7146->cal_flags == CAL_RET_NONE)
  966. fixed_threshold = ad7146_i2c_platform_data.fixed_th_full;
  967. else
  968. fixed_threshold = ad7146_i2c_platform_data.cal_fixed_th_full;
  969. data = (ad7146_i2c_platform_data.regs[4] & 0x0000ffff);
  970. sensitivity = (ad7146_i2c_platform_data.regs[3] & 0x0000ffff);
  971. threshold = (data / 4) + (((data - (data / 4)) / 16) * SS_LUT[((sensitivity & POS_SENS) >> 8)]) / 10000;
  972. threshold = (threshold * 194) / 100;
  973. ad7146->read(ad7146->dev, DRIVER_STG0_HIGH_THRESHOLD,
  974. &stg_th);
  975. ad7146->read(ad7146->dev, DRIVER_STG0_SF_AMBIENT,
  976. &stg_amb);
  977. return snprintf(buf, PAGE_SIZE, "%d,%d,%d\n", (unsigned int)fixed_threshold,
  978. (unsigned int)threshold, (unsigned int) (threshold * (100 - HYS_PERCENT) / 100));
  979. }
  980. static ssize_t ad7146_calibration_show(struct device *dev,
  981. struct device_attribute *attr, char *buf)
  982. {
  983. struct ad7146_chip *ad7146 = dev_get_drvdata(dev);
  984. return snprintf(buf, PAGE_SIZE, "%d\n", (unsigned int)ad7146->cal_flags);
  985. }
  986. static ssize_t ad7146_calibration_store(struct device *dev,
  987. struct device_attribute *attr, const char *buf, size_t size)
  988. {
  989. struct ad7146_chip *ad7146 = dev_get_drvdata(dev);
  990. int val, err;
  991. err = kstrtoint(buf, 10, &val);
  992. if (err < 0) {
  993. pr_err("[Grip]: %s, kstrtoint failed\n", __func__);
  994. } else {
  995. ad7146->during_cal_flags = DO_CALIBRATE;
  996. if (ad7146->eventcheck != 1)
  997. ad7146_hw_init(ad7146);
  998. if (val == 0)
  999. ad7146_calibration(ad7146, false);
  1000. else if (val == 1)
  1001. ad7146_calibration(ad7146, true);
  1002. else
  1003. pr_err("[Grip]: %s, kstrtoint %d\n", __func__, val);
  1004. ad7146->during_cal_flags = DONE_CALIBRATE;
  1005. if (ad7146->eventcheck == 1)
  1006. sensorgrip(ad7146);
  1007. else
  1008. sensorshutdownmode(ad7146);
  1009. }
  1010. return size;
  1011. }
  1012. static ssize_t ad7146_offset_show(struct device *dev,
  1013. struct device_attribute *attr, char *buf)
  1014. {
  1015. struct ad7146_chip *ad7146 = dev_get_drvdata(dev);
  1016. unsigned short offset;
  1017. short cal_offset;
  1018. if (ad7146->cal_flags != CAL_RET_NONE)
  1019. offset = ad7146_i2c_platform_data.cal_offset;
  1020. else
  1021. offset = (unsigned short)(ad7146_i2c_platform_data.regs[STAGE_AFE_OFFSET] & 0x0000ffff);
  1022. cal_offset = LD_POS_AFE_OFFSET(offset) + LD_NEG_AFE_OFFSET(offset);
  1023. return snprintf(buf, PAGE_SIZE, "%x,%d\n", offset, cal_offset);
  1024. }
  1025. static ssize_t ad7146_offset_store(struct device *dev,
  1026. struct device_attribute *attr, const char *buf, size_t size)
  1027. {
  1028. struct ad7146_chip *ad7146 = dev_get_drvdata(dev);
  1029. int err;
  1030. unsigned short data;
  1031. short val;
  1032. err = kstrtos16(buf, 10, &val);
  1033. if (err < 0) {
  1034. pr_err("[Grip]: %s, kstrtoint failed\n", __func__);
  1035. } else {
  1036. if (-MAX_OFFSET <= val && val <= MAX_OFFSET) {
  1037. pr_info("[Grip]: %s, %d\n", __func__, val);
  1038. if (val > 0)
  1039. data = ST_POS_AFE_OFFSET(val);
  1040. else
  1041. data = ST_NEG_AFE_OFFSET(val);
  1042. ad7146_i2c_platform_data.regs[STAGE_AFE_OFFSET] =
  1043. (ad7146_i2c_platform_data.regs[STAGE_AFE_OFFSET] & 0xFFFF0000)
  1044. + ((unsigned int)data & 0x0000FFFF);
  1045. ad7146->write(ad7146->dev, DRIVER_STG0_AFEOFFSET, data);
  1046. ad7146_force_cal(ad7146, SLEEP_TIME_TO_CALI_INT);
  1047. } else {
  1048. pr_err("[Grip]: %s, Error for data val(%d)\n", __func__, val);
  1049. }
  1050. }
  1051. return size;
  1052. }
  1053. static struct device_attribute dev_attr_sensor_name =
  1054. __ATTR(name, S_IRUSR | S_IRGRP,
  1055. ad7146_name_show, NULL);
  1056. static struct device_attribute dev_attr_sensor_vendor =
  1057. __ATTR(vendor, S_IRUSR | S_IRGRP,
  1058. ad7146_vendor_show, NULL);
  1059. static struct device_attribute dev_attr_sensor_raw_data =
  1060. __ATTR(raw_data, S_IRUSR | S_IRGRP,
  1061. ad7146_raw_data_show, NULL);
  1062. static struct device_attribute dev_attr_sensor_onoff =
  1063. __ATTR(onoff, S_IRUGO | S_IWUSR | S_IWGRP,
  1064. ad7146_onoff_show, ad7146_onoff_store);
  1065. static struct device_attribute dev_attr_sensor_threshold =
  1066. __ATTR(threshold, S_IRUGO | S_IWUSR | S_IWGRP,
  1067. ad7146_threshold_show, NULL);
  1068. static struct device_attribute dev_attr_sensor_calibration =
  1069. __ATTR(calibration, S_IRUGO | S_IWUSR | S_IWGRP,
  1070. ad7146_calibration_show, ad7146_calibration_store);
  1071. static struct device_attribute dev_attr_sensor_offset =
  1072. __ATTR(offset, S_IRUGO | S_IWUSR | S_IWGRP,
  1073. ad7146_offset_show, ad7146_offset_store);
  1074. static struct device_attribute *ad7146_attrs[] = {
  1075. &dev_attr_sensor_name,
  1076. &dev_attr_sensor_vendor,
  1077. &dev_attr_sensor_raw_data,
  1078. &dev_attr_sensor_onoff,
  1079. &dev_attr_sensor_threshold,
  1080. &dev_attr_sensor_calibration,
  1081. &dev_attr_sensor_offset,
  1082. &dev_attr_sensor_dump,
  1083. NULL,
  1084. };
  1085. static int ad7146_parse_dt(struct ad7146_chip *ad7146, struct device *dev)
  1086. {
  1087. struct device_node *dNode = dev->of_node;
  1088. enum of_gpio_flags flags;
  1089. if (dNode == NULL)
  1090. return -ENODEV;
  1091. ad7146->grip_int = of_get_named_gpio_flags(dNode,
  1092. "ad7146-i2c,grip_int-gpio", 0, &flags);
  1093. if (ad7146->grip_int < 0) {
  1094. pr_err("[Grip]: %s - get grip_int error\n", __func__);
  1095. return -ENODEV;
  1096. }
  1097. return 0;
  1098. }
  1099. /**
  1100. Device probe function
  1101. All initialization routines are handled here like the ISR registration,
  1102. Work creation,Input device registration,SYSFS attributes creation etc.
  1103. @param i2c_client the i2c structure of the ad7146 device/client.
  1104. @param i2c_device_id The i2c_device_id for the supported i2c device.
  1105. @return 0 on success,and On failure -ENOMEM, -EINVAL ,etc., will be returned
  1106. */
  1107. static int __devinit ad7146_probe(struct i2c_client *client,
  1108. const struct i2c_device_id *id)
  1109. {
  1110. int ret = -EINVAL;
  1111. struct input_dev *input = NULL;
  1112. struct device *dev = &client->dev;
  1113. struct ad7146_chip *ad7146 = NULL;
  1114. pr_info("[Grip]: %s: called", __func__);
  1115. if (client == NULL) {
  1116. pr_err("[Grip]: %s: Client doesn't exist\n", __func__);
  1117. return ret;
  1118. }
  1119. ad7146 = kzalloc(sizeof(struct ad7146_chip), GFP_KERNEL);
  1120. if (!ad7146) {
  1121. pr_err("[Grip]: %s: Memory allocation fail\n", __func__);
  1122. ret = -ENOMEM;
  1123. return ret;
  1124. }
  1125. ad7146->read = ad7146_i2c_read;
  1126. ad7146->write = ad7146_i2c_write;
  1127. ad7146->dev = dev;
  1128. ret = ad7146_parse_dt(ad7146, &client->dev);
  1129. if (ret < 0) {
  1130. pr_err("[Grip]: %s: - of_node error\n", __func__);
  1131. ret = -ENODEV;
  1132. goto err_of_node;
  1133. }
  1134. i2c_set_clientdata(client, ad7146);
  1135. INIT_WORK(&ad7146->work, ad7146_interrupt_thread);
  1136. /*
  1137. * Allocate and register ad7146 input device
  1138. */
  1139. input = input_allocate_device();
  1140. if (!input) {
  1141. pr_err("[Grip]: %s: could not allocate input device\n", __func__);
  1142. ret = -ENOMEM;
  1143. goto err_kzalloc_mem;
  1144. }
  1145. ad7146->input = input;
  1146. input_set_drvdata(ad7146->input, ad7146);
  1147. input->name = "grip_sensor";
  1148. set_bit(EV_REL, ad7146->input->evbit);
  1149. input_set_capability(ad7146->input, EV_REL, REL_MISC);
  1150. ret = input_register_device(ad7146->input);
  1151. if (ret) {
  1152. pr_err("[Grip]: %s: could not input_register_device(input);\n", __func__);
  1153. ret = -ENOMEM;
  1154. goto err_input_register_device;
  1155. }
  1156. ret = sensors_create_symlink(&ad7146->input->dev.kobj,
  1157. ad7146->input->name);
  1158. if (ret < 0) {
  1159. pr_err("[Grip]: %s: create_symlink error\n", __func__);
  1160. goto err_sensors_create_symlink;
  1161. }
  1162. ad7146->eventcheck = 0;
  1163. wake_lock_init(&ad7146->grip_wake_lock, WAKE_LOCK_SUSPEND, "grip_wake_lock");
  1164. ret = gpio_request(ad7146->grip_int, "gpio_grip_int");
  1165. pr_info("[Grip]: %s : gpio request %d\n", __func__, ad7146->grip_int);
  1166. if (ret < 0) {
  1167. pr_err("[Grip]: %s: gpio %d request failed (%d)\n",
  1168. __func__, ad7146->grip_int, ret);
  1169. goto err_gpio_request;
  1170. }
  1171. ret = gpio_direction_input(ad7146->grip_int);
  1172. if (ret < 0) {
  1173. pr_err("[Grip]: %s: failed to set gpio %d as input (%d)\n",
  1174. __func__, ad7146->grip_int, ret);
  1175. goto err_free_irq;
  1176. }
  1177. ad7146->irq = gpio_to_irq(ad7146->grip_int);
  1178. pr_info("[Grip]: %s: irq %d\n", __func__, ad7146->irq);
  1179. ret = request_threaded_irq(ad7146->irq, NULL, ad7146_isr,
  1180. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1181. dev_name(dev), ad7146);
  1182. if (ret) {
  1183. pr_err("[Grip]: %s: irq %d Driver init Failed", __func__, ad7146->irq);
  1184. goto err_free_irq;
  1185. }
  1186. disable_irq(ad7146->irq);
  1187. ret = sysfs_create_group(&input->dev.kobj, &ad7146_attr_group);
  1188. if (ret) {
  1189. pr_err("[Grip]: %s: cound not register sensor device\n", __func__);
  1190. goto err_sysfs_create_input;
  1191. }
  1192. ret = sensors_register(ad7146->grip_dev, ad7146, ad7146_attrs, "grip_sensor");
  1193. if (ret) {
  1194. pr_err("[Grip]: %s: cound not register sensor device\n", __func__);
  1195. goto err_sysfs_create_factory_grip;
  1196. }
  1197. #ifdef CONFIG_SENSOR_USE_SYMLINK
  1198. ret = sensors_initialize_symlink(input);
  1199. if (ret) {
  1200. pr_err("[Grip]: %s: cound not make grip sensor symlink(%d).\n",
  1201. __func__, ret);
  1202. goto out_sensors_initialize_symlink;
  1203. }
  1204. #endif
  1205. /* check if the device is existing by reading device id of AD7146 */
  1206. ret = ad7146_hw_detect(ad7146);
  1207. if (ret)
  1208. goto err_hw_detect;
  1209. /* initialize and request sw/hw resources */
  1210. sensorshutdownmode(ad7146);
  1211. ad7146->onoff_flags = 1;
  1212. pr_info("[Grip]: %s - probe done\n", __func__);
  1213. return 0;
  1214. err_hw_detect:
  1215. pr_err("[Grip]: %s: hw detect error\n", __func__);
  1216. #ifdef CONFIG_SENSOR_USE_SYMLINK
  1217. out_sensors_initialize_symlink:
  1218. #endif
  1219. err_sysfs_create_factory_grip:
  1220. sysfs_remove_group(&input->dev.kobj, &ad7146_attr_group);
  1221. err_sysfs_create_input:
  1222. free_irq(ad7146->irq, ad7146);
  1223. err_free_irq:
  1224. gpio_free(ad7146->grip_int);
  1225. err_gpio_request:
  1226. wake_lock_destroy(&ad7146->grip_wake_lock);
  1227. err_sensors_create_symlink:
  1228. input_unregister_device(input);
  1229. err_input_register_device:
  1230. input_free_device(input);
  1231. err_kzalloc_mem:
  1232. err_of_node:
  1233. kfree(ad7146);
  1234. pr_info("[Grip]: %s - probe failed(%d)\n", __func__, ret);
  1235. return ret;
  1236. }
  1237. /**
  1238. Removes the Device.
  1239. This is used to Remove the device or the I2C client from the system
  1240. @param client The Client Id to be removed
  1241. @return 0 on success
  1242. */
  1243. static int __devexit ad7146_i2c_remove(struct i2c_client *client)
  1244. {
  1245. struct ad7146_chip *ad7146 = i2c_get_clientdata(client);
  1246. pr_info("%s, Start\n", __func__);
  1247. if (ad7146 != NULL) {
  1248. if (ad7146->eventcheck == ENABLE_INTERRUPTS) {
  1249. disable_irq(ad7146->irq);
  1250. disable_irq_wake(ad7146->irq);
  1251. ad7146->write(ad7146->dev, STG_COM_INT_EN_REG, DISABLE_INT);
  1252. ad7146->write(ad7146->dev, STG_HIGH_INT_EN_REG, DISABLE_INT);
  1253. ad7146->write(ad7146->dev, STG_LOW_INT_EN_REG, DISABLE_INT);
  1254. msleep(SLEEP_TIME_TO_CALI_INT);
  1255. }
  1256. sysfs_remove_group(&ad7146->input->dev.kobj, &ad7146_attr_group);
  1257. free_irq(ad7146->irq, ad7146);
  1258. gpio_free(client->irq);
  1259. input_unregister_device(ad7146->input);
  1260. input_free_device(ad7146->input);
  1261. kfree(ad7146);
  1262. }
  1263. return 0;
  1264. }
  1265. void ad7146_shutdown(struct i2c_client *client)
  1266. {
  1267. struct ad7146_chip *ad7146 = i2c_get_clientdata(client);
  1268. pr_info("%s, Start\n", __func__);
  1269. if (ad7146 != NULL) {
  1270. if (ad7146->eventcheck == ENABLE_INTERRUPTS) {
  1271. disable_irq(ad7146->irq);
  1272. disable_irq_wake(ad7146->irq);
  1273. ad7146->write(ad7146->dev, STG_COM_INT_EN_REG, DISABLE_INT);
  1274. ad7146->write(ad7146->dev, STG_HIGH_INT_EN_REG, DISABLE_INT);
  1275. ad7146->write(ad7146->dev, STG_LOW_INT_EN_REG, DISABLE_INT);
  1276. msleep(SLEEP_TIME_TO_CALI_INT);
  1277. }
  1278. sysfs_remove_group(&ad7146->input->dev.kobj, &ad7146_attr_group);
  1279. free_irq(ad7146->irq, ad7146);
  1280. gpio_free(client->irq);
  1281. input_unregister_device(ad7146->input);
  1282. input_free_device(ad7146->input);
  1283. kfree(ad7146);
  1284. }
  1285. }
  1286. /**
  1287. Device ID table for the AD7146 driver
  1288. */
  1289. static int grip_i2c_suspend(struct device *dev)
  1290. {
  1291. struct i2c_client *client = to_i2c_client(dev);
  1292. struct ad7146_chip *ad7146 = i2c_get_clientdata(client);
  1293. pr_info("%s, check (%d)\n", __func__, ad7146->eventcheck);
  1294. /* if (ad7146->eventcheck == 0)
  1295. ad7146->hw->power_en(0);*/
  1296. return 0;
  1297. }
  1298. static int grip_i2c_resume(struct device *dev)
  1299. {
  1300. struct i2c_client *client = to_i2c_client(dev);
  1301. struct ad7146_chip *ad7146 = i2c_get_clientdata(client);
  1302. pr_info("%s, check (%d)\n", __func__, ad7146->eventcheck);
  1303. if (ad7146->eventcheck == 0) {
  1304. //ad7146->hw->power_en(1);
  1305. msleep(10);
  1306. sensorshutdownmode(ad7146);
  1307. }
  1308. return 0;
  1309. }
  1310. static struct of_device_id ad7146_match_table[] = {
  1311. { .compatible = "ad7146-i2c",},
  1312. {},
  1313. };
  1314. static const struct i2c_device_id ad7146_id[] = {
  1315. {"ad7146_match_table", 0},
  1316. {}
  1317. /*
  1318. { "ad7146_SAR_NORM", 0 },
  1319. { "ad7146_SAR_PROX", 1 },
  1320. { "ad7146_SAR", 2 }, {},
  1321. */
  1322. };
  1323. static const struct dev_pm_ops grip_dev_pm_ops = {
  1324. .suspend = grip_i2c_suspend,
  1325. .resume = grip_i2c_resume,
  1326. };
  1327. /**
  1328. The file Operation Table
  1329. */
  1330. struct i2c_driver ad7146_i2c_driver = {
  1331. .driver = {
  1332. .name = DRIVER_NAME,
  1333. .owner = THIS_MODULE,
  1334. .of_match_table = ad7146_match_table,
  1335. .pm = &grip_dev_pm_ops,
  1336. },
  1337. .probe = ad7146_probe,
  1338. .shutdown = ad7146_shutdown,
  1339. .remove = __devexit_p(ad7146_i2c_remove),
  1340. .id_table = ad7146_id,
  1341. };
  1342. /**
  1343. This is an init function called during module insertion --
  1344. calls in turn i2c driver probe function
  1345. */
  1346. static __init int ad7146_i2c_init(void)
  1347. {
  1348. pr_info("%s, start\n", __func__);
  1349. return i2c_add_driver(&ad7146_i2c_driver);
  1350. }
  1351. module_init(ad7146_i2c_init);
  1352. /**
  1353. Called during the module removal
  1354. */
  1355. static __exit void ad7146_i2c_exit(void)
  1356. {
  1357. i2c_del_driver(&ad7146_i2c_driver);
  1358. }
  1359. module_exit(ad7146_i2c_exit);
  1360. MODULE_DESCRIPTION("Analog Devices ad7146 MLD Driver");
  1361. MODULE_AUTHOR("Analog Devices");
  1362. MODULE_LICENSE("GPL");