lis2hh_acc.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068
  1. /******************** (C) COPYRIGHT 2013 STMicroelectronics ********************
  2. *
  3. * File Name : lis2hh_acc.c
  4. * Authors : AMS - Motion Mems Division - Application Team
  5. * : Matteo Dameno (matteo.dameno@st.com)
  6. * : Denis Ciocca (denis.ciocca@st.com)
  7. * : Both authors are willing to be considered the contact
  8. * : and update points for the driver.
  9. * Version : V.1.1.0
  10. * Date : 2013/Mar/28
  11. * Description : LIS2HH accelerometer sensor API
  12. *
  13. *******************************************************************************
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License version 2 as
  17. * published by the Free Software Foundation.
  18. *
  19. * THE PRESENT SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
  20. * OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, FOR THE SOLE
  21. * PURPOSE TO SUPPORT YOUR APPLICATION DEVELOPMENT.
  22. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  23. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  24. * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  25. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  26. *
  27. ******************************************************************************
  28. Revision 1.0.0 25/Feb/2013
  29. first revision
  30. supports sysfs;
  31. Revision 1.1.0 28/Mar/2013
  32. introduces hr_timers for polling;
  33. ******************************************************************************/
  34. #include <linux/err.h>
  35. #include <linux/errno.h>
  36. #include <linux/delay.h>
  37. #include <linux/fs.h>
  38. #include <linux/i2c.h>
  39. #include <linux/input.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/workqueue.h>
  42. #include <linux/irq.h>
  43. #include <linux/gpio.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/slab.h>
  46. #include <linux/kernel.h>
  47. #include <linux/device.h>
  48. #include <linux/module.h>
  49. #include <linux/moduleparam.h>
  50. #include <linux/of_gpio.h>
  51. #include <linux/math64.h>
  52. #include <linux/regulator/consumer.h>
  53. #include "sensors_core.h"
  54. #include "lis2hh.h"
  55. #define DEBUG 1
  56. #define VENDOR "STM"
  57. #define CHIP_ID "K2HH"
  58. #define MODULE_NAME "accelerometer_sensor"
  59. #define G_MAX 7995148 /* (SENSITIVITY_8G*(2^15-1)) */
  60. #define G_MIN - 7995392 /* (-SENSITIVITY_8G*(2^15) */
  61. #define FUZZ 0
  62. #define FLAT 0
  63. #define I2C_RETRY_DELAY 5
  64. #define I2C_RETRIES 5
  65. #define I2C_AUTO_INCREMENT (0x00)
  66. #define MS_TO_NS(x) (x*1000000L)
  67. #define SENSITIVITY_2G 61 /** ug/LSB */
  68. #define SENSITIVITY_4G 122 /** ug/LSB */
  69. #define SENSITIVITY_8G 244 /** ug/LSB */
  70. /* Accelerometer Sensor Operating Mode */
  71. #define LIS2HH_ACC_ENABLE (0x01)
  72. #define LIS2HH_ACC_DISABLE (0x00)
  73. #define AXISDATA_REG (0x28)
  74. #define WHOAMI_LIS2HH_ACC (0x41) /* Expctd content for WAI */
  75. #define ALL_ZEROES (0x00)
  76. #define LIS2HH_ACC_PM_OFF (0x00)
  77. #define ACC_ENABLE_ALL_AXES (0x07)
  78. /* Register Auto-increase */
  79. #define AC (1 << 7)
  80. /* CONTROL REGISTERS */
  81. #define TEMP_L (0x0B)
  82. #define TEMP_H (0x0C)
  83. #define WHO_AM_I (0x0F) /* WhoAmI register */
  84. #define ACT_THS (0x1E) /* Activity Threshold */
  85. #define ACT_DUR (0x1F) /* Activity Duration */
  86. /* ctrl 1: HR ODR2 ODR1 ODR0 BDU Zenable Yenable Xenable */
  87. #define CTRL1 (0x20) /* control reg 1 */
  88. #define CTRL2 (0x21) /* control reg 2 */
  89. #define CTRL3 (0x22) /* control reg 3 */
  90. #define CTRL4 (0x23) /* control reg 4 */
  91. #define CTRL5 (0x24) /* control reg 5 */
  92. #define CTRL6 (0x25) /* control reg 6 */
  93. #define CTRL7 (0x26) /* control reg 7 */
  94. #define STATUS_REG 0x27
  95. #define OUT_X_L 0x28
  96. #define OUT_X_H 0x29
  97. #define OUT_Y_L 0x2A
  98. #define OUT_Y_H 0x2B
  99. #define OUT_Z_L 0x2C
  100. #define OUT_Z_H 0x2D
  101. #define FIFO_CTRL (0x2E) /* fifo control reg */
  102. #define FIFO_SRC_REG 0x2F
  103. #define INT_CFG1 (0x30) /* interrupt 1 config */
  104. #define INT_SRC1 (0x31) /* interrupt 1 source */
  105. #define INT_THSX1 (0x32) /* interrupt 1 threshold x */
  106. #define INT_THSY1 (0x33) /* interrupt 1 threshold y */
  107. #define INT_THSZ1 (0x34) /* interrupt 1 threshold z */
  108. #define INT_DUR1 (0x35) /* interrupt 1 duration */
  109. #define INT_CFG2 (0x36) /* interrupt 2 config */
  110. #define INT_SRC2 (0x37) /* interrupt 2 source */
  111. #define INT_THS2 (0x38) /* interrupt 2 threshold */
  112. #define INT_DUR2 (0x39) /* interrupt 2 duration */
  113. #define REF_XL (0x3A) /* reference_l_x */
  114. #define REF_XH (0x3B) /* reference_h_x */
  115. #define REF_YL (0x3C) /* reference_l_y */
  116. #define REF_YH (0x3D) /* reference_h_y */
  117. #define REF_ZL (0x3E) /* reference_l_z */
  118. #define REF_ZH (0x3F) /* reference_h_z */
  119. /* end CONTROL REGISTRES */
  120. #define ACC_ODR10 (0x10) /* 10Hz output data rate */
  121. #define ACC_ODR50 (0x20) /* 50Hz output data rate */
  122. #define ACC_ODR100 (0x30) /* 100Hz output data rate */
  123. #define ACC_ODR200 (0x40) /* 200Hz output data rate */
  124. #define ACC_ODR400 (0x50) /* 400Hz output data rate */
  125. #define ACC_ODR800 (0x60) /* 800Hz output data rate */
  126. #define ACC_ODR_MASK (0X70)
  127. /* Registers configuration Mask and settings */
  128. /* CTRL1 */
  129. #define CTRL1_HR_DISABLE (0x00)
  130. #define CTRL1_HR_ENABLE (0x80)
  131. #define CTRL1_HR_MASK (0x80)
  132. #define CTRL1_BDU_ENABLE (0x08)
  133. #define CTRL1_BDU_MASK (0x08)
  134. /* CTRL2 */
  135. #define CTRL2_IG1_INT1 (0x08)
  136. /* CTRL3 */
  137. #define CTRL3_IG1_INT1 (0x08)
  138. #define CTRL3_DRDY_INT1
  139. /* CTRL4 */
  140. #define CTRL4_IF_ADD_INC_EN (0x04)
  141. #define CTRL4_BW_SCALE_ODR_AUT (0x00)
  142. #define CTRL4_BW_SCALE_ODR_SEL (0x08)
  143. #define CTRL4_ANTALIAS_BW_400 (0x00)
  144. #define CTRL4_ANTALIAS_BW_200 (0x40)
  145. #define CTRL4_ANTALIAS_BW_100 (0x80)
  146. #define CTRL4_ANTALIAS_BW_50 (0xC0)
  147. #define CTRL4_ANTALIAS_BW_MASK (0xC0)
  148. /* CTRL5 */
  149. #define CTRL5_HLACTIVE_L (0x02)
  150. #define CTRL5_HLACTIVE_H (0x00)
  151. /* CTRL6 */
  152. #define CTRL6_IG2_INT2 (0x10)
  153. #define CTRL6_DRDY_INT2 (0x01)
  154. /* CTRL7 */
  155. #define CTRL7_LIR2 (0x08)
  156. #define CTRL7_LIR1 (0x04)
  157. /* */
  158. #define NO_MASK (0xFF)
  159. #define INT1_DURATION_MASK (0x7F)
  160. #define INT1_THRESHOLD_MASK (0x7F)
  161. /* RESUME STATE INDICES */
  162. #define RES_CTRL1 0
  163. #define RES_CTRL2 1
  164. #define RES_CTRL3 2
  165. #define RES_CTRL4 3
  166. #define RES_CTRL5 4
  167. #define RES_CTRL6 5
  168. #define RES_CTRL7 6
  169. #define RES_INT_CFG1 7
  170. #define RES_INT_THSX1 8
  171. #define RES_INT_THSY1 9
  172. #define RES_INT_THSZ1 10
  173. #define RES_INT_DUR1 11
  174. #define RES_INT_CFG2 12
  175. #define RES_INT_THS2 13
  176. #define RES_INT_DUR2 14
  177. #define RES_TEMP_CFG_REG 15
  178. #define RES_REFERENCE_REG 16
  179. #define RES_FIFO_CTRL 17
  180. #define RESUME_ENTRIES 18
  181. #define CAL_DATA_AMOUNT 20
  182. /* end RESUME STATE INDICES */
  183. #define PM_OFF 0x00
  184. #define ODR400 0x70 /* 400Hz output data rate */
  185. #define CALIBRATION_FILE_PATH "/efs/calibration_data"
  186. #define OUTPUT_ALWAYS_ANTI_ALIASED 1
  187. #define DEFAULT_POWER_ON_SETTING (ODR400 | STATUS_REG)
  188. #define SELF_TEST_2G_MAX_LSB (24576)
  189. #define SELF_TEST_2G_MIN_LSB (1146)
  190. struct {
  191. unsigned int cutoff_ms;
  192. unsigned int mask;
  193. } lis2hh_acc_odr_table[] = {
  194. { 2, ACC_ODR800 },
  195. { 3, ACC_ODR400 },
  196. { 5, ACC_ODR200 },
  197. { 10, ACC_ODR100 },
  198. #if(!OUTPUT_ALWAYS_ANTI_ALIASED)
  199. { 20, ACC_ODR50 },
  200. { 100, ACC_ODR10 },
  201. #endif
  202. };
  203. struct k2hh_acc {
  204. s16 x;
  205. s16 y;
  206. s16 z;
  207. };
  208. static int int1_gpio = LIS2HH_ACC_DEFAULT_INT1_GPIO;
  209. static int int2_gpio = LIS2HH_ACC_DEFAULT_INT2_GPIO;
  210. module_param(int1_gpio, int, S_IRUGO);
  211. module_param(int2_gpio, int, S_IRUGO);
  212. struct lis2hh_acc_status {
  213. struct i2c_client *client;
  214. struct lis2hh_acc_platform_data *pdata;
  215. struct mutex lock;
  216. struct work_struct input_poll_work;
  217. struct hrtimer hr_timer_poll;
  218. ktime_t polling_ktime;
  219. struct workqueue_struct *hr_timer_poll_work_queue;
  220. struct input_dev *input_dev;
  221. int hw_initialized;
  222. /* hw_working=-1 means not tested yet */
  223. int hw_working;
  224. atomic_t enable;
  225. int on_before_suspend;
  226. int use_smbus;
  227. u8 sensitivity;
  228. u8 resume_state[RESUME_ENTRIES];
  229. struct device *dev;
  230. int irq1;
  231. struct work_struct irq1_work;
  232. struct workqueue_struct *irq1_work_queue;
  233. int irq2;
  234. struct work_struct irq2_work;
  235. struct workqueue_struct *irq2_work_queue;
  236. struct regulator *l19;
  237. struct regulator *lvs1_1p8;
  238. struct k2hh_acc cal_data;
  239. #ifdef DEBUG
  240. u8 reg_addr;
  241. #endif
  242. };
  243. /* sets default init values to be written in registers at probe stage */
  244. static void lis2hh_acc_set_init_register_values(struct lis2hh_acc_status *stat)
  245. {
  246. memset(stat->resume_state, 0, ARRAY_SIZE(stat->resume_state));
  247. stat->resume_state[RES_CTRL1] = (ALL_ZEROES | \
  248. CTRL1_HR_DISABLE | \
  249. CTRL1_BDU_ENABLE | \
  250. ACC_ENABLE_ALL_AXES);
  251. if(stat->pdata->gpio_int1 >= 0)
  252. stat->resume_state[RES_CTRL3] =
  253. (stat->resume_state[RES_CTRL3] | \
  254. CTRL3_IG1_INT1);
  255. stat->resume_state[RES_CTRL4] = (ALL_ZEROES | \
  256. CTRL4_IF_ADD_INC_EN);
  257. stat->resume_state[RES_CTRL5] = (ALL_ZEROES | \
  258. CTRL5_HLACTIVE_H);
  259. if(stat->pdata->gpio_int2 >= 0)
  260. stat->resume_state[RES_CTRL6] =
  261. (stat->resume_state[RES_CTRL6] | \
  262. CTRL6_IG2_INT2);
  263. stat->resume_state[RES_CTRL7] = (ALL_ZEROES | \
  264. CTRL7_LIR2 | CTRL7_LIR1);
  265. }
  266. int k2hh_power_on(struct lis2hh_acc_status *data, bool onoff)
  267. {
  268. int ret = -1;
  269. if (!data->l19) {
  270. data->l19 = regulator_get(&data->client->dev, "8226_l19");
  271. if (!data->l19) {
  272. pr_err("%s: regulator pointer null l19, rc=%d\n",
  273. __func__, ret);
  274. return ret;
  275. }
  276. ret = regulator_set_voltage(data->l19, 2850000, 2850000);
  277. if (ret) {
  278. pr_err("%s: set voltage failed on l19, rc=%d\n",
  279. __func__, ret);
  280. return ret;
  281. }
  282. }
  283. if (!data->lvs1_1p8) {
  284. data->lvs1_1p8 = regulator_get(&data->client->dev, "8226_lvs1");
  285. if(!data->lvs1_1p8){
  286. pr_err("%s: regulator_get for 8226_lvs1 failed\n", __func__);
  287. return 0;
  288. }
  289. }
  290. if(onoff){
  291. ret = regulator_enable(data->l19);
  292. if (ret) {
  293. pr_err("%s: Failed to enable regulator l19.\n",
  294. __func__);
  295. return ret;
  296. }
  297. ret = regulator_enable(data->lvs1_1p8);
  298. if (ret) {
  299. pr_err("%s: Failed to enable regulator lvs1_1p8.\n",
  300. __func__);
  301. return ret;
  302. }
  303. }
  304. else {
  305. ret = regulator_disable(data->l19);
  306. if (ret) {
  307. pr_err("%s: Failed to disable regulatorl19.\n",
  308. __func__);
  309. return ret;
  310. }
  311. ret = regulator_enable(data->lvs1_1p8);
  312. if (ret) {
  313. pr_err("%s: Failed to disable regulator lvs1_1p8.\n",
  314. __func__);
  315. return ret;
  316. }
  317. }
  318. return 0;
  319. }
  320. static int lis2hh_acc_i2c_read(struct lis2hh_acc_status *stat, u8 *buf,
  321. int len)
  322. {
  323. int ret;
  324. u8 reg = buf[0];
  325. u8 cmd = reg;
  326. unsigned int ii;
  327. if (len > 1)
  328. cmd = (I2C_AUTO_INCREMENT | reg);
  329. if (stat->use_smbus) {
  330. if (len == 1) {
  331. ret = i2c_smbus_read_byte_data(stat->client, cmd);
  332. buf[0] = ret & 0xff;
  333. #ifdef DEBUG
  334. dev_warn(&stat->client->dev,
  335. "i2c_smbus_read_byte_data: ret=0x%02x, len:%d ,"
  336. "command=0x%02x, buf[0]=0x%02x\n",
  337. ret, len, cmd , buf[0]);
  338. #endif
  339. } else if (len > 1) {
  340. ret = i2c_smbus_read_i2c_block_data(stat->client,
  341. cmd, len, buf);
  342. #ifdef DEBUG
  343. dev_warn(&stat->client->dev,
  344. "i2c_smbus_read_i2c_block_data: ret:%d len:%d, "
  345. "command=0x%02x, ",
  346. ret, len, cmd);
  347. for (ii = 0; ii < len; ii++)
  348. printk(KERN_DEBUG "buf[%d]=0x%02x,",
  349. ii, buf[ii]);
  350. printk("\n");
  351. #endif
  352. } else
  353. ret = -1;
  354. if (ret < 0) {
  355. dev_err(&stat->client->dev,
  356. "read transfer error: len:%d, command=0x%02x\n",
  357. len, cmd);
  358. return 0; /* failure */
  359. }
  360. return len; /* success */
  361. }
  362. ret = i2c_master_send(stat->client, &cmd, sizeof(cmd));
  363. if (ret != sizeof(cmd))
  364. return ret;
  365. return i2c_master_recv(stat->client, buf, len);
  366. }
  367. static int lis2hh_acc_i2c_write(struct lis2hh_acc_status *stat, u8 *buf,
  368. int len)
  369. {
  370. int ret;
  371. u8 reg, value;
  372. unsigned int ii;
  373. if (len > 1)
  374. buf[0] = (I2C_AUTO_INCREMENT | buf[0]);
  375. reg = buf[0];
  376. value = buf[1];
  377. if (stat->use_smbus) {
  378. if (len == 1) {
  379. ret = i2c_smbus_write_byte_data(stat->client,
  380. reg, value);
  381. #ifdef DEBUG
  382. dev_warn(&stat->client->dev,
  383. "i2c_smbus_write_byte_data: ret=%d, len:%d, "
  384. "command=0x%02x, value=0x%02x\n",
  385. ret, len, reg , value);
  386. #endif
  387. return ret;
  388. } else if (len > 1) {
  389. ret = i2c_smbus_write_i2c_block_data(stat->client,
  390. reg, len, buf + 1);
  391. #ifdef DEBUG
  392. dev_warn(&stat->client->dev,
  393. "i2c_smbus_write_i2c_block_data: ret=%d, "
  394. "len:%d, command=0x%02x, ",
  395. ret, len, reg);
  396. for (ii = 0; ii < (len + 1); ii++)
  397. printk(KERN_DEBUG "value[%d]=0x%02x,",
  398. ii, buf[ii]);
  399. printk("\n");
  400. #endif
  401. return ret;
  402. }
  403. }
  404. ret = i2c_master_send(stat->client, buf, len+1);
  405. return (ret == len+1) ? 0 : ret;
  406. }
  407. static int lis2hh_acc_hw_init(struct lis2hh_acc_status *stat)
  408. {
  409. int err = -1;
  410. u8 buf[7];
  411. pr_info("%s: hw init start\n", LIS2HH_ACC_DEV_NAME);
  412. buf[0] = WHO_AM_I;
  413. err = lis2hh_acc_i2c_read(stat, buf, 1);
  414. if (err < 0) {
  415. dev_warn(&stat->client->dev, "Error reading WHO_AM_I:"
  416. " is device available/working?\n");
  417. goto err_firstread;
  418. } else
  419. stat->hw_working = 1;
  420. if (buf[0] != WHOAMI_LIS2HH_ACC) {
  421. dev_err(&stat->client->dev,
  422. "device unknown. Expected: 0x%02x,"
  423. " Replies: 0x%02x\n",
  424. WHOAMI_LIS2HH_ACC, buf[0]);
  425. err = -1; /* choose the right coded error */
  426. goto err_unknown_device;
  427. }
  428. buf[0] = FIFO_CTRL;
  429. buf[1] = stat->resume_state[RES_FIFO_CTRL];
  430. err = lis2hh_acc_i2c_write(stat, buf, 1);
  431. if (err < 0)
  432. goto err_resume_state;
  433. buf[0] = INT_THSX1;
  434. buf[1] = stat->resume_state[RES_INT_THSX1];
  435. buf[2] = stat->resume_state[RES_INT_THSY1];
  436. buf[3] = stat->resume_state[RES_INT_THSZ1];
  437. buf[4] = stat->resume_state[RES_INT_DUR1];
  438. err = lis2hh_acc_i2c_write(stat, buf, 4);
  439. if (err < 0)
  440. goto err_resume_state;
  441. buf[0] = INT_CFG1;
  442. buf[1] = stat->resume_state[RES_INT_CFG1];
  443. err = lis2hh_acc_i2c_write(stat, buf, 1);
  444. if (err < 0)
  445. goto err_resume_state;
  446. buf[0] = CTRL2;
  447. buf[1] = stat->resume_state[RES_CTRL2];
  448. buf[2] = stat->resume_state[RES_CTRL3];
  449. buf[3] = stat->resume_state[RES_CTRL4];
  450. buf[4] = stat->resume_state[RES_CTRL5];
  451. buf[5] = stat->resume_state[RES_CTRL6];
  452. buf[6] = stat->resume_state[RES_CTRL7];
  453. err = lis2hh_acc_i2c_write(stat, buf, 6);
  454. if (err < 0)
  455. goto err_resume_state;
  456. buf[0] = CTRL1;
  457. buf[1] = stat->resume_state[RES_CTRL1];
  458. err = lis2hh_acc_i2c_write(stat, buf, 1);
  459. if (err < 0)
  460. goto err_resume_state;
  461. stat->hw_initialized = 1;
  462. pr_info("%s: hw init done\n", LIS2HH_ACC_DEV_NAME);
  463. return 0;
  464. err_firstread:
  465. stat->hw_working = 0;
  466. err_unknown_device:
  467. err_resume_state:
  468. stat->hw_initialized = 0;
  469. dev_err(&stat->client->dev, "hw init error 0x%02x,0x%02x: %d\n", buf[0],
  470. buf[1], err);
  471. return err;
  472. }
  473. static void lis2hh_acc_device_power_off(struct lis2hh_acc_status *stat)
  474. {
  475. int err;
  476. u8 buf[2] = { CTRL1, LIS2HH_ACC_PM_OFF };
  477. err = lis2hh_acc_i2c_write(stat, buf, 1);
  478. if (err < 0)
  479. dev_err(&stat->client->dev, "soft power off failed: %d\n", err);
  480. if (stat->pdata->power_off) {
  481. if (stat->pdata->gpio_int1 >= 0)
  482. disable_irq_nosync(stat->irq1);
  483. if (stat->pdata->gpio_int2 >= 0)
  484. disable_irq_nosync(stat->irq2);
  485. stat->pdata->power_off();
  486. stat->hw_initialized = 0;
  487. }
  488. if (stat->hw_initialized) {
  489. if (stat->pdata->gpio_int1 >= 0)
  490. disable_irq_nosync(stat->irq1);
  491. if (stat->pdata->gpio_int2 >= 0)
  492. disable_irq_nosync(stat->irq2);
  493. stat->hw_initialized = 0;
  494. }
  495. }
  496. static int lis2hh_acc_device_power_on(struct lis2hh_acc_status *stat)
  497. {
  498. int err = -1;
  499. if (stat->pdata->power_on) {
  500. err = stat->pdata->power_on();
  501. if (err < 0) {
  502. dev_err(&stat->client->dev,
  503. "power_on failed: %d\n", err);
  504. return err;
  505. }
  506. if (stat->pdata->gpio_int1 >= 0)
  507. enable_irq(stat->irq1);
  508. if (stat->pdata->gpio_int2 >= 0)
  509. enable_irq(stat->irq2);
  510. }
  511. mdelay(30);
  512. if (!stat->hw_initialized) {
  513. err = lis2hh_acc_hw_init(stat);
  514. if (stat->hw_working == 1 && err < 0) {
  515. lis2hh_acc_device_power_off(stat);
  516. return err;
  517. }
  518. }
  519. if (stat->hw_initialized) {
  520. if (stat->pdata->gpio_int1 >= 0)
  521. enable_irq(stat->irq1);
  522. if (stat->pdata->gpio_int2 >= 0)
  523. enable_irq(stat->irq2);
  524. }
  525. return 0;
  526. }
  527. static int lis2hh_acc_update_fs_range(struct lis2hh_acc_status *stat,
  528. u8 new_fs_range)
  529. {
  530. int err = -1;
  531. u8 sensitivity;
  532. u8 buf[2];
  533. u8 updated_val;
  534. u8 init_val;
  535. u8 new_val;
  536. u8 mask = LIS2HH_ACC_FS_MASK;
  537. switch (new_fs_range) {
  538. case LIS2HH_ACC_FS_2G:
  539. sensitivity = SENSITIVITY_2G;
  540. break;
  541. case LIS2HH_ACC_FS_4G:
  542. sensitivity = SENSITIVITY_4G;
  543. break;
  544. case LIS2HH_ACC_FS_8G:
  545. sensitivity = SENSITIVITY_8G;
  546. break;
  547. default:
  548. dev_err(&stat->client->dev, "invalid fs range requested: %u\n",
  549. new_fs_range);
  550. return -EINVAL;
  551. }
  552. /* Updates configuration register 4,
  553. * which contains fs range setting */
  554. buf[0] = CTRL4;
  555. err = lis2hh_acc_i2c_read(stat, buf, 1);
  556. if (err < 0)
  557. goto error;
  558. init_val = buf[0];
  559. stat->resume_state[RES_CTRL4] = init_val;
  560. new_val = new_fs_range;
  561. updated_val = ((mask & new_val) | ((~mask) & init_val));
  562. buf[1] = updated_val;
  563. buf[0] = CTRL4;
  564. err = lis2hh_acc_i2c_write(stat, buf, 1);
  565. if (err < 0)
  566. goto error;
  567. stat->resume_state[RES_CTRL4] = updated_val;
  568. stat->sensitivity = sensitivity;
  569. return err;
  570. error:
  571. dev_err(&stat->client->dev,
  572. "update fs range failed 0x%02x,0x%02x: %d\n",
  573. buf[0], buf[1], err);
  574. return err;
  575. }
  576. static int lis2hh_acc_update_odr(struct lis2hh_acc_status *stat,
  577. int poll_interval_ms)
  578. {
  579. int err;
  580. int i;
  581. u8 config[2];
  582. u8 updated_val;
  583. u8 init_val;
  584. u8 new_val;
  585. u8 mask = ACC_ODR_MASK;
  586. /* Following, looks for the longest possible odr interval scrolling the
  587. * odr_table vector from the end (shortest interval) backward (longest
  588. * interval), to support the poll_interval requested by the system.
  589. * It must be the longest interval lower then the poll interval.*/
  590. for (i = ARRAY_SIZE(lis2hh_acc_odr_table) - 1; i >= 0; i--) {
  591. if ((lis2hh_acc_odr_table[i].cutoff_ms <= poll_interval_ms)
  592. || (i == 0))
  593. break;
  594. }
  595. new_val = lis2hh_acc_odr_table[i].mask;
  596. /* Updates configuration register 1,
  597. * which contains odr range setting if enabled,
  598. * otherwise updates RES_CTRL1 for when it will */
  599. if (atomic_read(&stat->enable)) {
  600. config[0] = CTRL1;
  601. err = lis2hh_acc_i2c_read(stat, config, 1);
  602. if (err < 0)
  603. goto error;
  604. init_val = config[0];
  605. stat->resume_state[RES_CTRL1] = init_val;
  606. updated_val = ((mask & new_val) | ((~mask) & init_val));
  607. config[1] = updated_val;
  608. config[0] = CTRL1;
  609. err = lis2hh_acc_i2c_write(stat, config, 1);
  610. if (err < 0)
  611. goto error;
  612. stat->resume_state[RES_CTRL1] = updated_val;
  613. return err;
  614. } else {
  615. init_val = stat->resume_state[RES_CTRL1];
  616. updated_val = ((mask & new_val) | ((~mask) & init_val));
  617. stat->resume_state[RES_CTRL1] = updated_val;
  618. return 0;
  619. }
  620. error:
  621. dev_err(&stat->client->dev,
  622. "update odr failed 0x%02x,0x%02x: %d\n",
  623. config[0], config[1], err);
  624. return err;
  625. }
  626. static int lis2hh_acc_register_write(struct lis2hh_acc_status *stat,
  627. u8 *buf, u8 reg_address, u8 new_value)
  628. {
  629. int err = -1;
  630. /* Sets configuration register at reg_address
  631. * NOTE: this is a straight overwrite */
  632. buf[0] = reg_address;
  633. buf[1] = new_value;
  634. err = lis2hh_acc_i2c_write(stat, buf, 1);
  635. if (err < 0)
  636. return err;
  637. return err;
  638. }
  639. static int lis2hh_acc_get_data(
  640. struct lis2hh_acc_status *stat, int *xyz)
  641. {
  642. int err = -1;
  643. /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
  644. u8 acc_data[6];
  645. /* x,y,z hardware data */
  646. s32 hw_d[3] = { 0 };
  647. mutex_lock(&stat->lock);
  648. acc_data[0] = (AXISDATA_REG);
  649. err = lis2hh_acc_i2c_read(stat, acc_data, 6);
  650. mutex_unlock(&stat->lock);
  651. if (err < 0)
  652. return err;
  653. hw_d[0] = ((s16) ((acc_data[1] << 8) | acc_data[0]));
  654. hw_d[1] = ((s16) ((acc_data[3] << 8) | acc_data[2]));
  655. hw_d[2] = ((s16) ((acc_data[5] << 8) | acc_data[4]));
  656. #if 0
  657. hw_d[0] = hw_d[0] * stat->sensitivity;
  658. hw_d[1] = hw_d[1] * stat->sensitivity;
  659. hw_d[2] = hw_d[2] * stat->sensitivity;
  660. #endif
  661. xyz[0] = ((stat->pdata->negate_x) ? (-hw_d[stat->pdata->axis_map_x])
  662. : (hw_d[stat->pdata->axis_map_x]));
  663. xyz[1] = ((stat->pdata->negate_y) ? (-hw_d[stat->pdata->axis_map_y])
  664. : (hw_d[stat->pdata->axis_map_y]));
  665. xyz[2] = ((stat->pdata->negate_z) ? (-hw_d[stat->pdata->axis_map_z])
  666. : (hw_d[stat->pdata->axis_map_z]));
  667. xyz[0] = xyz[0] - stat->cal_data.x ;
  668. xyz[1] = xyz[1] - stat->cal_data.y ;
  669. xyz[2] = xyz[2] - stat->cal_data.z ;
  670. /*printk("%s read x=%d, y=%d, z=%d\n",
  671. LIS2HH_ACC_DEV_NAME, xyz[0], xyz[1], xyz[2]);*/
  672. #ifdef DEBUG
  673. dev_dbg(&stat->client->dev,"%s read x=%d, y=%d, z=%d\n",
  674. LIS2HH_ACC_DEV_NAME, xyz[0], xyz[1], xyz[2]);
  675. #endif
  676. return err;
  677. }
  678. static void lis2hh_acc_report_values(struct lis2hh_acc_status *stat,
  679. int *xyz)
  680. {
  681. input_report_rel(stat->input_dev, REL_X, xyz[0]);
  682. input_report_rel(stat->input_dev, REL_Y, xyz[1]);
  683. input_report_rel(stat->input_dev, REL_Z, xyz[2]);
  684. input_sync(stat->input_dev);
  685. }
  686. static void lis2hh_acc_report_triple(struct lis2hh_acc_status *stat)
  687. {
  688. int err;
  689. int xyz[3];
  690. err = lis2hh_acc_get_data(stat, xyz);
  691. if (err < 0)
  692. dev_err(&stat->client->dev, "get_data failed\n");
  693. else
  694. lis2hh_acc_report_values(stat, xyz);
  695. }
  696. static irqreturn_t lis2hh_acc_isr1(int irq, void *dev)
  697. {
  698. struct lis2hh_acc_status *stat = dev;
  699. printk("lis2hh_acc lis2hh_acc_isr1 called END \n");
  700. disable_irq_nosync(irq);
  701. queue_work(stat->irq1_work_queue, &stat->irq1_work);
  702. printk("%s: isr1 queued %d \n", LIS2HH_ACC_DEV_NAME,stat->irq1);
  703. printk("lis2hh_acc lis2hh_acc_isr1 called END \n");
  704. return IRQ_HANDLED;
  705. }
  706. static irqreturn_t lis2hh_acc_isr2(int irq, void *dev)
  707. {
  708. struct lis2hh_acc_status *stat = dev;
  709. disable_irq_nosync(irq);
  710. queue_work(stat->irq2_work_queue, &stat->irq2_work);
  711. pr_debug("%s: isr2 queued\n", LIS2HH_ACC_DEV_NAME);
  712. return IRQ_HANDLED;
  713. }
  714. static void lis2hh_acc_irq1_work_func(struct work_struct *work)
  715. {
  716. struct lis2hh_acc_status *stat =
  717. container_of(work, struct lis2hh_acc_status, irq1_work);
  718. /* TODO add interrupt service procedure.
  719. ie:lis2hh_acc_get_int1_source(stat); */
  720. /* ; */
  721. pr_debug("%s: IRQ1 served\n", LIS2HH_ACC_DEV_NAME);
  722. /* exit: */
  723. enable_irq(stat->irq1);
  724. }
  725. static void lis2hh_acc_irq2_work_func(struct work_struct *work)
  726. {
  727. struct lis2hh_acc_status *stat =
  728. container_of(work, struct lis2hh_acc_status, irq2_work);
  729. /* TODO add interrupt service procedure.
  730. ie:lis2hh_acc_get_tap_source(stat); */
  731. /* ; */
  732. pr_debug("%s: IRQ2 served\n", LIS2HH_ACC_DEV_NAME);
  733. /* exit: */
  734. enable_irq(stat->irq2);
  735. }
  736. static int k2hh_open_calibration(struct lis2hh_acc_status *data)
  737. {
  738. struct file *cal_filp = NULL;
  739. int err = 0;
  740. mm_segment_t old_fs;
  741. old_fs = get_fs();
  742. set_fs(KERNEL_DS);
  743. cal_filp = filp_open(CALIBRATION_FILE_PATH, O_RDONLY, 0666);
  744. if (IS_ERR(cal_filp)) {
  745. err = PTR_ERR(cal_filp);
  746. if (err != -ENOENT)
  747. pr_err("%s: Can't open calibration file\n", __func__);
  748. set_fs(old_fs);
  749. return err;
  750. }
  751. err = cal_filp->f_op->read(cal_filp,
  752. (char *)&data->cal_data, 3 * sizeof(s16), &cal_filp->f_pos);
  753. if (err != 3 * sizeof(s16)) {
  754. pr_err("%s: Can't read the cal data from file\n", __func__);
  755. err = -EIO;
  756. }
  757. printk("%s: (%d,%d,%d)\n", __func__,
  758. data->cal_data.x, data->cal_data.y, data->cal_data.z);
  759. filp_close(cal_filp, current->files);
  760. set_fs(old_fs);
  761. return err;
  762. }
  763. static int lis2hh_acc_enable(struct lis2hh_acc_status *stat)
  764. {
  765. int err;
  766. if (!atomic_cmpxchg(&stat->enable, 0, 1)) {
  767. err = lis2hh_acc_device_power_on(stat);
  768. if (err < 0) {
  769. atomic_set(&stat->enable, 0);
  770. return err;
  771. }
  772. err = k2hh_open_calibration(stat);
  773. if (err < 0 && err != -ENOENT)
  774. pr_err("%s: k2hh_open_calibration() failed\n",
  775. __func__);
  776. stat->polling_ktime = ktime_set(stat->pdata->poll_interval / 1000,
  777. MS_TO_NS(stat->pdata->poll_interval % 1000));
  778. hrtimer_start(&stat->hr_timer_poll,
  779. stat->polling_ktime, HRTIMER_MODE_REL);
  780. }
  781. return 0;
  782. }
  783. static int lis2hh_acc_disable(struct lis2hh_acc_status *stat)
  784. {
  785. if (atomic_cmpxchg(&stat->enable, 1, 0)) {
  786. cancel_work_sync(&stat->input_poll_work);
  787. lis2hh_acc_device_power_off(stat);
  788. }
  789. return 0;
  790. }
  791. static ssize_t read_single_reg(struct device *dev, char *buf, u8 reg)
  792. {
  793. ssize_t ret;
  794. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  795. int err;
  796. u8 data = reg;
  797. err = lis2hh_acc_i2c_read(stat, &data, 1);
  798. if (err < 0)
  799. return err;
  800. ret = sprintf(buf, "0x%02x\n", data);
  801. return ret;
  802. }
  803. static int write_reg(struct device *dev, const char *buf, u8 reg,
  804. u8 mask, int resumeIndex)
  805. {
  806. int err = -1;
  807. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  808. u8 x[2];
  809. u8 new_val;
  810. unsigned long val;
  811. if (strict_strtoul(buf, 16, &val))
  812. return -EINVAL;
  813. new_val = ((u8) val & mask);
  814. x[0] = reg;
  815. x[1] = new_val;
  816. err = lis2hh_acc_register_write(stat, x, reg, new_val);
  817. if (err < 0)
  818. return err;
  819. stat->resume_state[resumeIndex] = new_val;
  820. return err;
  821. }
  822. static ssize_t attr_get_polling_rate(struct device *dev,
  823. struct device_attribute *attr,
  824. char *buf)
  825. {
  826. int val;
  827. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  828. mutex_lock(&stat->lock);
  829. val = stat->pdata->poll_interval;
  830. mutex_unlock(&stat->lock);
  831. return sprintf(buf, "%d\n", val);
  832. }
  833. static ssize_t attr_set_polling_rate(struct device *dev,
  834. struct device_attribute *attr,
  835. const char *buf, size_t size)
  836. {
  837. int err;
  838. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  839. int64_t nsdelay;
  840. unsigned long interval_ms;
  841. if (kstrtoll(buf, 10, &nsdelay))
  842. return -EINVAL;
  843. if (!nsdelay)
  844. return -EINVAL;
  845. interval_ms = div_s64(nsdelay, 1000000);
  846. interval_ms = max((unsigned int)interval_ms, stat->pdata->min_interval);
  847. mutex_lock(&stat->lock);
  848. stat->pdata->poll_interval = interval_ms;
  849. err = lis2hh_acc_update_odr(stat, interval_ms);
  850. if(err >= 0) {
  851. stat->pdata->poll_interval = interval_ms;
  852. stat->polling_ktime = ktime_set(stat->pdata->poll_interval / 1000,
  853. MS_TO_NS(stat->pdata->poll_interval % 1000));
  854. }
  855. mutex_unlock(&stat->lock);
  856. return size;
  857. }
  858. static ssize_t attr_get_range(struct device *dev,
  859. struct device_attribute *attr, char *buf)
  860. {
  861. char val;
  862. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  863. char range = 2;
  864. mutex_lock(&stat->lock);
  865. val = stat->pdata->fs_range ;
  866. switch (val) {
  867. case LIS2HH_ACC_FS_2G:
  868. range = 2;
  869. break;
  870. case LIS2HH_ACC_FS_4G:
  871. range = 4;
  872. break;
  873. case LIS2HH_ACC_FS_8G:
  874. range = 8;
  875. break;
  876. }
  877. mutex_unlock(&stat->lock);
  878. return sprintf(buf, "%d\n", range);
  879. }
  880. static ssize_t attr_set_range(struct device *dev,
  881. struct device_attribute *attr,
  882. const char *buf, size_t size)
  883. {
  884. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  885. unsigned long val;
  886. u8 range;
  887. int err;
  888. if (strict_strtoul(buf, 10, &val))
  889. return -EINVAL;
  890. switch (val) {
  891. case 2:
  892. range = LIS2HH_ACC_FS_2G;
  893. break;
  894. case 4:
  895. range = LIS2HH_ACC_FS_4G;
  896. break;
  897. case 8:
  898. range = LIS2HH_ACC_FS_8G;
  899. break;
  900. default:
  901. dev_err(&stat->client->dev, "invalid range request: %lu,"
  902. " discarded\n", val);
  903. return -EINVAL;
  904. }
  905. mutex_lock(&stat->lock);
  906. err = lis2hh_acc_update_fs_range(stat, range);
  907. if (err < 0) {
  908. mutex_unlock(&stat->lock);
  909. return err;
  910. }
  911. stat->pdata->fs_range = range;
  912. mutex_unlock(&stat->lock);
  913. dev_info(&stat->client->dev, "range set to: %lu g\n", val);
  914. return size;
  915. }
  916. static ssize_t attr_get_enable(struct device *dev,
  917. struct device_attribute *attr, char *buf)
  918. {
  919. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  920. int val = atomic_read(&stat->enable);
  921. return sprintf(buf, "%d\n", val);
  922. }
  923. static ssize_t attr_set_enable(struct device *dev,
  924. struct device_attribute *attr,
  925. const char *buf, size_t size)
  926. {
  927. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  928. unsigned long val;
  929. if (strict_strtoul(buf, 10, &val))
  930. return -EINVAL;
  931. if (val)
  932. lis2hh_acc_enable(stat);
  933. else
  934. lis2hh_acc_disable(stat);
  935. return size;
  936. }
  937. static ssize_t attr_set_intconfig1(struct device *dev,
  938. struct device_attribute *attr, const char *buf, size_t size)
  939. {
  940. return write_reg(dev, buf, INT_CFG1, NO_MASK, RES_INT_CFG1);
  941. }
  942. static ssize_t attr_get_intconfig1(struct device *dev,
  943. struct device_attribute *attr, char *buf)
  944. {
  945. return read_single_reg(dev, buf, INT_CFG1);
  946. }
  947. static ssize_t attr_set_duration1(struct device *dev,
  948. struct device_attribute *attr, const char *buf, size_t size)
  949. {
  950. return write_reg(dev, buf, INT_DUR1, INT1_DURATION_MASK, RES_INT_DUR1);
  951. }
  952. static ssize_t attr_get_duration1(struct device *dev,
  953. struct device_attribute *attr, char *buf)
  954. {
  955. return read_single_reg(dev, buf, INT_DUR1);
  956. }
  957. static ssize_t attr_set_threshx1(struct device *dev,
  958. struct device_attribute *attr, const char *buf, size_t size)
  959. {
  960. return write_reg(dev, buf, INT_THSX1, INT1_THRESHOLD_MASK, RES_INT_THSX1);
  961. }
  962. static ssize_t attr_get_threshx1(struct device *dev,
  963. struct device_attribute *attr, char *buf)
  964. {
  965. return read_single_reg(dev, buf, INT_THSX1);
  966. }
  967. static ssize_t attr_set_threshy1(struct device *dev,
  968. struct device_attribute *attr, const char *buf, size_t size)
  969. {
  970. return write_reg(dev, buf, INT_THSY1, INT1_THRESHOLD_MASK, RES_INT_THSY1);
  971. }
  972. static ssize_t attr_get_threshy1(struct device *dev,
  973. struct device_attribute *attr, char *buf)
  974. {
  975. return read_single_reg(dev, buf, INT_THSY1);
  976. }
  977. static ssize_t attr_set_threshz1(struct device *dev,
  978. struct device_attribute *attr, const char *buf, size_t size)
  979. {
  980. return write_reg(dev, buf, INT_THSZ1, INT1_THRESHOLD_MASK, RES_INT_THSZ1);
  981. }
  982. static ssize_t attr_get_threshz1(struct device *dev,
  983. struct device_attribute *attr, char *buf)
  984. {
  985. return read_single_reg(dev, buf, INT_THSZ1);
  986. }
  987. static ssize_t attr_get_source1(struct device *dev,
  988. struct device_attribute *attr, char *buf)
  989. {
  990. return read_single_reg(dev, buf, INT_SRC1);
  991. }
  992. #ifdef DEBUG
  993. /* PAY ATTENTION: These DEBUG functions don't manage resume_state */
  994. static ssize_t attr_reg_set(struct device *dev, struct device_attribute *attr,
  995. const char *buf, size_t size)
  996. {
  997. int rc;
  998. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  999. u8 x[2];
  1000. unsigned long val;
  1001. if (strict_strtoul(buf, 16, &val))
  1002. return -EINVAL;
  1003. mutex_lock(&stat->lock);
  1004. x[0] = stat->reg_addr;
  1005. mutex_unlock(&stat->lock);
  1006. x[1] = val;
  1007. rc = lis2hh_acc_i2c_write(stat, x, 1);
  1008. /*TODO: error need to be managed */
  1009. if (rc < 0) {
  1010. pr_err("%s: lis2hh_acc_i2c_write() failed\n", __func__);
  1011. return rc;
  1012. }
  1013. return size;
  1014. }
  1015. static ssize_t attr_reg_get(struct device *dev, struct device_attribute *attr,
  1016. char *buf)
  1017. {
  1018. ssize_t ret;
  1019. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  1020. int rc;
  1021. u8 data;
  1022. mutex_lock(&stat->lock);
  1023. data = stat->reg_addr;
  1024. mutex_unlock(&stat->lock);
  1025. rc = lis2hh_acc_i2c_read(stat, &data, 1);
  1026. /*TODO: error need to be managed */
  1027. if (rc < 0) {
  1028. pr_err("%s: lis2hh_acc_i2c_read() failed\n", __func__);
  1029. return rc;
  1030. }
  1031. ret = sprintf(buf, "0x%02x\n", data);
  1032. return ret;
  1033. }
  1034. static ssize_t attr_addr_set(struct device *dev, struct device_attribute *attr,
  1035. const char *buf, size_t size)
  1036. {
  1037. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  1038. unsigned long val;
  1039. if (strict_strtoul(buf, 16, &val))
  1040. return -EINVAL;
  1041. mutex_lock(&stat->lock);
  1042. stat->reg_addr = val;
  1043. mutex_unlock(&stat->lock);
  1044. return size;
  1045. }
  1046. #endif
  1047. static ssize_t k2hh_accel_vendor_show(struct device *dev,
  1048. struct device_attribute *attr, char *buf)
  1049. {
  1050. return sprintf(buf, "%s\n", VENDOR);
  1051. }
  1052. static ssize_t k2hh_accel_name_show(struct device *dev,
  1053. struct device_attribute *attr, char *buf)
  1054. {
  1055. return sprintf(buf, "%s\n", CHIP_ID);
  1056. }
  1057. static ssize_t k2hh_fs_read(struct device *dev,
  1058. struct device_attribute *attr, char *buf)
  1059. {
  1060. struct lis2hh_acc_status *data = dev_get_drvdata(dev);
  1061. int xyz[3];
  1062. lis2hh_acc_get_data(data,xyz);
  1063. return sprintf(buf, "%d,%d,%d\n", xyz[0], xyz[1], xyz[2]);
  1064. }
  1065. static ssize_t k2hh_calibration_show(struct device *dev,
  1066. struct device_attribute *attr,
  1067. char *buf)
  1068. {
  1069. int err;
  1070. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  1071. err = k2hh_open_calibration(stat);
  1072. if (err < 0)
  1073. pr_err("%s: k2hh_open_calibration() failed\n", __func__);
  1074. if (!stat->cal_data.x && !stat->cal_data.y && !stat->cal_data.z)
  1075. err = -1;
  1076. return sprintf(buf, "%d %d %d %d\n",
  1077. err, stat->cal_data.x, stat->cal_data.y, stat->cal_data.z);
  1078. }
  1079. static int k2hh_do_calibrate(struct device *dev, bool do_calib)
  1080. {
  1081. struct lis2hh_acc_status *acc_data = dev_get_drvdata(dev);
  1082. struct file *cal_filp = NULL;
  1083. int sum[3] = { 0, };
  1084. int err = 0;
  1085. int i;
  1086. mm_segment_t old_fs;
  1087. int xyz[3];
  1088. if (do_calib) {
  1089. acc_data->cal_data.x = 0;
  1090. acc_data->cal_data.y = 0;
  1091. acc_data->cal_data.z = 0;
  1092. for (i = 0; i < CAL_DATA_AMOUNT; i++) {
  1093. err = lis2hh_acc_get_data(acc_data,xyz);
  1094. if (err < 0) {
  1095. pr_err("%s: lis2hh_acc_get_data() "
  1096. "failed in the %dth loop\n",
  1097. __func__, i);
  1098. return err;
  1099. }
  1100. sum[0] += xyz[0];
  1101. sum[1] += xyz[1];
  1102. sum[2] += xyz[2];
  1103. }
  1104. acc_data->cal_data.x = sum[0] / CAL_DATA_AMOUNT;
  1105. acc_data->cal_data.y = sum[1] / CAL_DATA_AMOUNT;
  1106. if (sum[2] >= 0)
  1107. acc_data->cal_data.z = (sum[2] / CAL_DATA_AMOUNT)-16384;
  1108. else
  1109. acc_data->cal_data.z = (sum[2] / CAL_DATA_AMOUNT)+16384;
  1110. } else {
  1111. acc_data->cal_data.x = 0;
  1112. acc_data->cal_data.y = 0;
  1113. acc_data->cal_data.z = 0;
  1114. }
  1115. printk(KERN_INFO "%s: cal data (%d,%d,%d)\n", __func__,
  1116. acc_data->cal_data.x, acc_data->cal_data.y, acc_data->cal_data.z);
  1117. old_fs = get_fs();
  1118. set_fs(KERNEL_DS);
  1119. cal_filp = filp_open(CALIBRATION_FILE_PATH,
  1120. O_CREAT | O_TRUNC | O_WRONLY, 0666);
  1121. if (IS_ERR(cal_filp)) {
  1122. pr_err("%s: Can't open calibration file\n", __func__);
  1123. set_fs(old_fs);
  1124. err = PTR_ERR(cal_filp);
  1125. return err;
  1126. }
  1127. err = cal_filp->f_op->write(cal_filp,
  1128. (char *)&acc_data->cal_data, 3 * sizeof(s16), &cal_filp->f_pos);
  1129. if (err != 3 * sizeof(s16)) {
  1130. pr_err("%s: Can't write the cal data to file\n", __func__);
  1131. err = -EIO;
  1132. }
  1133. filp_close(cal_filp, current->files);
  1134. set_fs(old_fs);
  1135. return err;
  1136. }
  1137. static ssize_t k2hh_calibration_store(struct device *dev,
  1138. struct device_attribute *attr,
  1139. const char *buf, size_t count)
  1140. {
  1141. bool do_calib;
  1142. int err;
  1143. if (sysfs_streq(buf, "1"))
  1144. do_calib = true;
  1145. else if (sysfs_streq(buf, "0"))
  1146. do_calib = false;
  1147. else {
  1148. pr_debug("%s: invalid value %d\n", __func__, *buf);
  1149. return -EINVAL;
  1150. }
  1151. err = k2hh_do_calibrate(dev, do_calib);
  1152. if (err < 0) {
  1153. pr_err("%s: k2hh_do_calibrate() failed\n", __func__);
  1154. return err;
  1155. }
  1156. return count;
  1157. }
  1158. static ssize_t attr_get_selftest(struct device *dev,
  1159. struct device_attribute *attr, char *buf)
  1160. {
  1161. struct lis2hh_acc_status *stat = dev_get_drvdata(dev);
  1162. int val, i, en_state = 0;
  1163. ssize_t ret;
  1164. u8 x[8];
  1165. s32 NO_ST[3] = {0, 0, 0};
  1166. s32 ST[3] = {0, 0, 0};
  1167. en_state = atomic_read(&stat->enable);
  1168. lis2hh_acc_disable(stat);
  1169. lis2hh_acc_device_power_on(stat);
  1170. x[0] = CTRL1;
  1171. x[1] = 0x3f;
  1172. lis2hh_acc_i2c_write(stat, x, 1);
  1173. x[0] = CTRL4;
  1174. x[1] = 0x04;
  1175. x[2] = 0x00;
  1176. x[3] = 0x00;
  1177. lis2hh_acc_i2c_write(stat, x, 3);
  1178. mdelay(80);
  1179. x[0] = AXISDATA_REG;
  1180. lis2hh_acc_i2c_read(stat, x, 6);
  1181. for (i = 0; i < 5; i++) {
  1182. while (1) {
  1183. x[0] = 0x27;
  1184. val = lis2hh_acc_i2c_read(stat, x, 1);
  1185. if (val < 0) {
  1186. ret = sprintf(buf, "I2C fail. (%d)\n", val);
  1187. goto ST_EXIT;
  1188. }
  1189. if (x[0] & 0x08)
  1190. break;
  1191. }
  1192. x[0] = AXISDATA_REG;
  1193. lis2hh_acc_i2c_read(stat, x, 6);
  1194. NO_ST[0] += (s16)(x[1] << 8 | x[0]);
  1195. NO_ST[1] += (s16)(x[3] << 8 | x[2]);
  1196. NO_ST[2] += (s16)(x[5] << 8 | x[4]);
  1197. }
  1198. NO_ST[0] /= 5;
  1199. NO_ST[1] /= 5;
  1200. NO_ST[2] /= 5;
  1201. x[0] = CTRL5;
  1202. x[1] = 0x04;
  1203. lis2hh_acc_i2c_write(stat, x, 1);
  1204. mdelay(80);
  1205. x[0] = AXISDATA_REG;
  1206. lis2hh_acc_i2c_read(stat, x, 6);
  1207. for (i = 0; i < 5; i++) {
  1208. while (1) {
  1209. x[0] = 0x27;
  1210. val = lis2hh_acc_i2c_read(stat, x, 1);
  1211. if (val < 0) {
  1212. ret = sprintf(buf, "I2C fail. (%d)\n", val);
  1213. goto ST_EXIT;
  1214. }
  1215. if (x[0] & 0x08)
  1216. break;
  1217. }
  1218. x[0] = AXISDATA_REG;
  1219. lis2hh_acc_i2c_read(stat, x, 6);
  1220. ST[0] += (s16)(x[1] << 8 | x[0]);
  1221. ST[1] += (s16)(x[3] << 8 | x[2]);
  1222. ST[2] += (s16)(x[5] << 8 | x[4]);
  1223. }
  1224. ST[0] /= 5;
  1225. ST[1] /= 5;
  1226. ST[2] /= 5;
  1227. for (val = 1, i = 0; i < 3; i++) {
  1228. ST[i] -= NO_ST[i];
  1229. ST[i] = abs(ST[i]);
  1230. if ((SELF_TEST_2G_MIN_LSB > ST[i]) || (ST[i] > SELF_TEST_2G_MAX_LSB)) {
  1231. pr_info("ST[%d]: Out of range!! (%d)\n", i, ST[i]);
  1232. val = 0;
  1233. }
  1234. }
  1235. if (val)
  1236. ret = sprintf(buf, "1, %d, %d, %d \n", ST[0], ST[1], ST[2]);
  1237. else
  1238. ret = sprintf(buf, "0, %d, %d, %d \n", ST[0], ST[1], ST[2]);
  1239. ST_EXIT:
  1240. x[0] = CTRL1;
  1241. x[1] = 0x00;
  1242. lis2hh_acc_i2c_write(stat, x, 1);
  1243. x[0] = CTRL5;
  1244. x[1] = 0x00;
  1245. lis2hh_acc_i2c_write(stat, x, 1);
  1246. lis2hh_acc_device_power_off(stat);
  1247. if (en_state) lis2hh_acc_enable(stat);
  1248. return ret;
  1249. }
  1250. /*
  1251. static ssize_t
  1252. k2hh_accel_position_show(struct device *dev,
  1253. struct device_attribute *attr,
  1254. char *buf)
  1255. {
  1256. struct lis2hh_acc_status *data = dev_get_drvdata(dev);
  1257. return sprintf(buf, "%d\n", data->position);
  1258. }
  1259. static ssize_t
  1260. k2hh_accel_position_store(struct device *dev,
  1261. struct device_attribute *attr,
  1262. const char *buf,
  1263. size_t count)
  1264. {
  1265. struct lis2hh_acc_status *data = dev_get_drvdata(dev);
  1266. int err = 0;
  1267. err = kstrtoint(buf, 10, &data->position);
  1268. if (err < 0)
  1269. pr_err("%s, kstrtoint failed.", __func__);
  1270. return count;
  1271. }*/
  1272. static DEVICE_ATTR(name, 0664, k2hh_accel_name_show, NULL);
  1273. static DEVICE_ATTR(vendor, 0664, k2hh_accel_vendor_show, NULL);
  1274. static DEVICE_ATTR(range, 0664, attr_get_range, attr_set_range);
  1275. static DEVICE_ATTR(int1_config, 0664, attr_get_intconfig1, attr_set_intconfig1);
  1276. static DEVICE_ATTR(int1_duration, 0664, attr_get_duration1, attr_set_duration1);
  1277. static DEVICE_ATTR(int1_thresholdx, 0664, attr_get_threshx1, attr_set_threshx1);
  1278. static DEVICE_ATTR(int1_thresholdy, 0664, attr_get_threshy1, attr_set_threshy1);
  1279. static DEVICE_ATTR(int1_thresholdz, 0664, attr_get_threshz1, attr_set_threshz1);
  1280. static DEVICE_ATTR(int1_source, 0444, attr_get_source1, NULL);
  1281. static DEVICE_ATTR(raw_data, 0664, k2hh_fs_read, NULL);
  1282. static DEVICE_ATTR(calibration, 0664, k2hh_calibration_show, k2hh_calibration_store);
  1283. static DEVICE_ATTR(selftest, 0444, attr_get_selftest, NULL);
  1284. #ifdef DEBUG
  1285. static DEVICE_ATTR(reg_value, 0600, attr_reg_get, attr_reg_set);
  1286. static DEVICE_ATTR(reg_addr, 0200, NULL, attr_addr_set);
  1287. #endif
  1288. static DEVICE_ATTR(enable,S_IRUGO | S_IWUSR | S_IWGRP,attr_get_enable, attr_set_enable);
  1289. static DEVICE_ATTR(poll_delay,S_IRUGO | S_IWUSR | S_IWGRP,attr_get_polling_rate, attr_set_polling_rate);
  1290. static struct attribute *k2hh_attributes[] = {
  1291. &dev_attr_enable.attr,
  1292. &dev_attr_poll_delay.attr,
  1293. NULL
  1294. };
  1295. static struct attribute_group k2hh_attribute_group = {
  1296. .attrs = k2hh_attributes
  1297. };
  1298. static struct device_attribute *sensor_attrs[] = {
  1299. &dev_attr_name,
  1300. &dev_attr_vendor,
  1301. &dev_attr_range,
  1302. &dev_attr_int1_config,
  1303. &dev_attr_int1_duration,
  1304. &dev_attr_int1_thresholdx,
  1305. &dev_attr_int1_thresholdy,
  1306. &dev_attr_int1_thresholdz,
  1307. &dev_attr_int1_source,
  1308. &dev_attr_raw_data,
  1309. &dev_attr_calibration,
  1310. &dev_attr_selftest,
  1311. #ifdef DEBUG
  1312. &dev_attr_reg_value,
  1313. &dev_attr_reg_addr,
  1314. #endif
  1315. NULL,
  1316. };
  1317. /*
  1318. static int create_sysfs_interfaces(struct device *dev)
  1319. {
  1320. int i;
  1321. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  1322. if (device_create_file(dev, attributes + i))
  1323. goto error;
  1324. return 0;
  1325. error:
  1326. for ( ; i >= 0; i--)
  1327. device_remove_file(dev, attributes + i);
  1328. dev_err(dev, "%s:Unable to create interface\n", __func__);
  1329. return -1;
  1330. }
  1331. static int remove_sysfs_interfaces(struct device *dev)
  1332. {
  1333. int i;
  1334. for (i = 0; i < ARRAY_SIZE(attributes); i++)
  1335. device_remove_file(dev, attributes + i);
  1336. return 0;
  1337. }
  1338. */
  1339. static void lis2hh_acc_input_poll_work_func(struct work_struct *work)
  1340. {
  1341. struct lis2hh_acc_status *stat;
  1342. stat = container_of((struct work_struct *) work,
  1343. struct lis2hh_acc_status, input_poll_work);
  1344. lis2hh_acc_report_triple(stat);
  1345. if (atomic_read(&stat->enable))
  1346. hrtimer_start(&stat->hr_timer_poll, stat->polling_ktime, HRTIMER_MODE_REL);
  1347. }
  1348. enum hrtimer_restart lis2hh_acc_hr_timer_poll_function(struct hrtimer *timer)
  1349. {
  1350. struct lis2hh_acc_status *stat;
  1351. stat = container_of((struct hrtimer *)timer,
  1352. struct lis2hh_acc_status, hr_timer_poll);
  1353. queue_work(stat->hr_timer_poll_work_queue, &stat->input_poll_work);
  1354. return HRTIMER_NORESTART;
  1355. }
  1356. #if 0
  1357. int lis2hh_acc_input_open(struct input_dev *input)
  1358. {
  1359. struct lis2hh_acc_status *stat = input_get_drvdata(input);
  1360. dev_dbg(&stat->client->dev, "%s\n", __func__);
  1361. return lis2hh_acc_enable(stat);
  1362. }
  1363. void lis2hh_acc_input_close(struct input_dev *dev)
  1364. {
  1365. struct lis2hh_acc_status *stat = input_get_drvdata(dev);
  1366. dev_dbg(&stat->client->dev, "%s\n", __func__);
  1367. lis2hh_acc_disable(stat);
  1368. }
  1369. #endif
  1370. static int lis2hh_acc_validate_pdata(struct lis2hh_acc_status *stat)
  1371. {
  1372. /* checks for correctness of minimal polling period */
  1373. stat->pdata->min_interval =
  1374. max((unsigned int)LIS2HH_ACC_MIN_POLL_PERIOD_MS,
  1375. stat->pdata->min_interval);
  1376. stat->pdata->poll_interval = max(stat->pdata->poll_interval,
  1377. stat->pdata->min_interval);
  1378. if (stat->pdata->axis_map_x > 2 ||
  1379. stat->pdata->axis_map_y > 2 ||
  1380. stat->pdata->axis_map_z > 2) {
  1381. dev_err(&stat->client->dev, "invalid axis_map value "
  1382. "x:%u y:%u z%u\n", stat->pdata->axis_map_x,
  1383. stat->pdata->axis_map_y,
  1384. stat->pdata->axis_map_z);
  1385. return -EINVAL;
  1386. }
  1387. /* Only allow 0 and 1 for negation boolean flag */
  1388. if (stat->pdata->negate_x > 1 || stat->pdata->negate_y > 1
  1389. || stat->pdata->negate_z > 1) {
  1390. dev_err(&stat->client->dev, "invalid negate value "
  1391. "x:%u y:%u z:%u\n", stat->pdata->negate_x,
  1392. stat->pdata->negate_y, stat->pdata->negate_z);
  1393. return -EINVAL;
  1394. }
  1395. /* Enforce minimum polling interval */
  1396. if (stat->pdata->poll_interval < stat->pdata->min_interval) {
  1397. dev_err(&stat->client->dev, "minimum poll interval violated\n");
  1398. return -EINVAL;
  1399. }
  1400. return 0;
  1401. }
  1402. static int lis2hh_acc_input_init(struct lis2hh_acc_status *stat)
  1403. {
  1404. int err;
  1405. INIT_WORK(&stat->input_poll_work, lis2hh_acc_input_poll_work_func);
  1406. stat->input_dev = input_allocate_device();
  1407. if (!stat->input_dev) {
  1408. err = -ENOMEM;
  1409. dev_err(&stat->client->dev, "input device allocation failed\n");
  1410. goto err0;
  1411. }
  1412. #if 0
  1413. stat->input_dev->open = lis2hh_acc_input_open;
  1414. stat->input_dev->close = lis2hh_acc_input_close;
  1415. #endif
  1416. //stat->input_dev->name = LIS2HH_ACC_DEV_NAME;
  1417. stat->input_dev->name = "accelerometer_sensor";
  1418. stat->input_dev->id.bustype = BUS_I2C;
  1419. //stat->input_dev->dev.parent = &stat->client->dev;
  1420. input_set_capability(stat->input_dev, EV_REL, REL_X);
  1421. input_set_capability(stat->input_dev, EV_REL, REL_Y);
  1422. input_set_capability(stat->input_dev, EV_REL, REL_Z);
  1423. input_set_drvdata(stat->input_dev, stat);
  1424. #if 0
  1425. set_bit(EV_ABS, stat->input_dev->evbit);
  1426. /* next is used for interruptA sources data if the case */
  1427. set_bit(ABS_MISC, stat->input_dev->absbit);
  1428. /* next is used for interruptB sources data if the case */
  1429. set_bit(ABS_WHEEL, stat->input_dev->absbit);
  1430. input_set_abs_params(stat->input_dev, ABS_X, G_MIN, G_MAX, FUZZ, FLAT);
  1431. input_set_abs_params(stat->input_dev, ABS_Y, G_MIN, G_MAX, FUZZ, FLAT);
  1432. input_set_abs_params(stat->input_dev, ABS_Z, G_MIN, G_MAX, FUZZ, FLAT);
  1433. /* next is used for interruptA sources data if the case */
  1434. input_set_abs_params(stat->input_dev, ABS_MISC, INT_MIN, INT_MAX, 0, 0);
  1435. /* next is used for interruptB sources data if the case */
  1436. input_set_abs_params(stat->input_dev, ABS_WHEEL, INT_MIN,
  1437. INT_MAX, 0, 0);
  1438. #endif
  1439. err = input_register_device(stat->input_dev);
  1440. if (err) {
  1441. dev_err(&stat->client->dev,
  1442. "unable to register input device %s\n",
  1443. stat->input_dev->name);
  1444. goto err1;
  1445. }
  1446. err = sensors_create_symlink(&stat->input_dev->dev.kobj, stat->input_dev->name);
  1447. if (err < 0) {
  1448. input_unregister_device(stat->input_dev);
  1449. return err;
  1450. }
  1451. /* Setup sysfs */
  1452. err =sysfs_create_group(&stat->input_dev->dev.kobj,&k2hh_attribute_group);
  1453. if (err < 0)
  1454. {
  1455. sensors_remove_symlink(&stat->input_dev->dev.kobj,stat->input_dev->name);
  1456. input_unregister_device(stat->input_dev);
  1457. return err;
  1458. }
  1459. return 0;
  1460. err1:
  1461. input_free_device(stat->input_dev);
  1462. err0:
  1463. return err;
  1464. }
  1465. static void lis2hh_acc_input_cleanup(struct lis2hh_acc_status *stat)
  1466. {
  1467. input_unregister_device(stat->input_dev);
  1468. input_free_device(stat->input_dev);
  1469. }
  1470. #ifdef CONFIG_OF
  1471. /* device tree parsing function */
  1472. static int k2hh_parse_dt(struct device *dev,
  1473. struct lis2hh_acc_platform_data *pdata)
  1474. {
  1475. unsigned int poll_interval;
  1476. unsigned int min_interval;
  1477. u32 axis_map_x;
  1478. u32 axis_map_y;
  1479. u32 axis_map_z;
  1480. u32 negate_x;
  1481. u32 negate_y;
  1482. u32 negate_z;
  1483. struct device_node *dNode = dev->of_node;
  1484. if (dNode == NULL)
  1485. return -ENODEV;
  1486. pdata->gpio_int1 = of_get_named_gpio_flags(dNode, "stm,irq_gpio", 0, &pdata->int_flags);
  1487. of_property_read_u32(dNode,"stm,axis_map_x" ,&axis_map_x);
  1488. of_property_read_u32(dNode,"stm,axis_map_y" ,&axis_map_y);
  1489. of_property_read_u32(dNode,"stm,axis_map_z" ,&axis_map_z);
  1490. of_property_read_u32(dNode,"stm,negate_x" ,&negate_x);
  1491. of_property_read_u32(dNode,"stm,negate_y" ,&negate_y);
  1492. of_property_read_u32(dNode,"stm,negate_z" ,&negate_z);
  1493. of_property_read_u32(dNode,"stm,poll_interval" ,&poll_interval);
  1494. of_property_read_u32(dNode,"stm,min_interval" ,&min_interval);
  1495. pdata->fs_range = LIS2HH_ACC_FS_2G;
  1496. pdata->axis_map_x=axis_map_x;
  1497. pdata->axis_map_y = axis_map_y;
  1498. pdata->axis_map_z = axis_map_z;
  1499. pdata->negate_x = negate_x;
  1500. pdata->negate_y = negate_y;
  1501. pdata->negate_z = negate_z;
  1502. pdata->poll_interval = poll_interval;
  1503. pdata->min_interval = min_interval;
  1504. pdata->gpio_int2 = -1;
  1505. if (pdata->gpio_int1 < 0) {
  1506. pr_err("[SENSOR]: %s - get irq_gpio error\n", __func__);
  1507. return -ENODEV;
  1508. }
  1509. printk(KERN_INFO "%s pull-up:%d \n", __func__, pdata->gpio_int1);
  1510. return 0;
  1511. }
  1512. #else
  1513. static int k2hh_parse_dt(struct device *dev,
  1514. struct lis2hh_acc_platform_data)
  1515. {
  1516. return -ENODEV;
  1517. }
  1518. #endif
  1519. static int lis2hh_acc_probe(struct i2c_client *client,
  1520. const struct i2c_device_id *id)
  1521. {
  1522. struct lis2hh_acc_status *stat;
  1523. struct lis2hh_acc_platform_data *pdata=NULL;
  1524. u32 smbus_func = (I2C_FUNC_SMBUS_BYTE_DATA |
  1525. I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_I2C_BLOCK);
  1526. int err = -1;
  1527. dev_info(&client->dev, "probe start.\n");
  1528. if (client-> dev.of_node) {
  1529. pdata = devm_kzalloc (&client->dev ,
  1530. sizeof (struct lis2hh_acc_platform_data), GFP_KERNEL);
  1531. if (!pdata) {
  1532. dev_err(&client->dev, "Failed to allocate memory\n");
  1533. return -ENOMEM;
  1534. }
  1535. err = k2hh_parse_dt(&client->dev, pdata);
  1536. pr_info("%s: x=%d , y= %d , z = %d \n", __func__,
  1537. pdata->axis_map_x, pdata->axis_map_y, pdata->axis_map_z);
  1538. if (err) {
  1539. printk("%s err_free_pdata \n",__func__ );
  1540. goto err_free_pdata;
  1541. }
  1542. } else {
  1543. pdata = client->dev.platform_data;
  1544. dev_err(&client->dev,
  1545. "%s: K2hh failed to align dtsi", __func__);
  1546. }
  1547. if (!pdata)
  1548. return -EINVAL;
  1549. stat = kzalloc(sizeof(struct lis2hh_acc_status), GFP_KERNEL);
  1550. if (stat == NULL) {
  1551. dev_err(&client->dev,
  1552. "failed to allocate memory for module data\n");
  1553. err = -ENOMEM;
  1554. printk("%s exit \n",__func__ );
  1555. goto exit;
  1556. }
  1557. stat->use_smbus = 0;
  1558. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1559. dev_warn(&client->dev, "client not i2c capable\n");
  1560. if (i2c_check_functionality(client->adapter, smbus_func)) {
  1561. stat->use_smbus = 1;
  1562. dev_warn(&client->dev, "client using SMBUS\n");
  1563. } else {
  1564. err = -ENODEV;
  1565. dev_err(&client->dev, "client nor SMBUS capable\n");
  1566. goto exit_check_functionality_failed;
  1567. }
  1568. }
  1569. mutex_init(&stat->lock);
  1570. mutex_lock(&stat->lock);
  1571. stat->client = client;
  1572. i2c_set_clientdata(client, stat);
  1573. k2hh_power_on(stat,1);
  1574. stat->pdata= pdata;
  1575. stat->hr_timer_poll_work_queue = 0;
  1576. err = lis2hh_acc_validate_pdata(stat);
  1577. if (err < 0) {
  1578. dev_err(&client->dev, "failed to validate platform data\n");
  1579. goto err_mutexunlock;
  1580. }
  1581. if (stat->pdata->init) {
  1582. err = stat->pdata->init();
  1583. if (err < 0) {
  1584. dev_err(&client->dev, "init failed: %d\n", err);
  1585. goto err_pdata_init;
  1586. }
  1587. }
  1588. if (stat->pdata->gpio_int1 >= 0) {
  1589. stat->irq1 = gpio_to_irq(stat->pdata->gpio_int1);
  1590. pr_info("%s: %s has set irq1 to irq: %d, "
  1591. "mapped on gpio:%d\n",
  1592. LIS2HH_ACC_DEV_NAME, __func__, stat->irq1,
  1593. stat->pdata->gpio_int1);
  1594. }
  1595. if (stat->pdata->gpio_int2 >= 0) {
  1596. stat->irq2 = gpio_to_irq(stat->pdata->gpio_int2);
  1597. pr_info("%s: %s has set irq2 to irq: %d, "
  1598. "mapped on gpio:%d\n",
  1599. LIS2HH_ACC_DEV_NAME, __func__, stat->irq2,
  1600. stat->pdata->gpio_int2);
  1601. }
  1602. lis2hh_acc_set_init_register_values(stat);
  1603. err = lis2hh_acc_device_power_on(stat);
  1604. if (err < 0) {
  1605. dev_err(&client->dev, "power on failed: %d\n", err);
  1606. goto err_pdata_init;
  1607. }
  1608. atomic_set(&stat->enable, 1);
  1609. err = lis2hh_acc_update_fs_range(stat, stat->pdata->fs_range);
  1610. if (err < 0) {
  1611. dev_err(&client->dev, "update_fs_range failed\n");
  1612. goto err_power_off;
  1613. }
  1614. err = lis2hh_acc_update_odr(stat, stat->pdata->poll_interval);
  1615. if (err < 0) {
  1616. dev_err(&client->dev, "update_odr failed\n");
  1617. goto err_power_off;
  1618. }
  1619. stat->hr_timer_poll_work_queue = create_workqueue("lis2hh_acc_hr_timer_poll_wq");
  1620. hrtimer_init(&stat->hr_timer_poll, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1621. stat->hr_timer_poll.function = &lis2hh_acc_hr_timer_poll_function;
  1622. err=sensors_register(stat->dev, stat, sensor_attrs, MODULE_NAME);
  1623. if (err < 0)
  1624. goto err_power_off;
  1625. //need to WORK CAK
  1626. err = lis2hh_acc_input_init(stat);
  1627. if (err < 0) {
  1628. dev_err(&client->dev, "input init failed\n");
  1629. goto err_remove_hr_work_queue;
  1630. }
  1631. lis2hh_acc_device_power_off(stat);
  1632. /* As default, do not report information */
  1633. atomic_set(&stat->enable, 0);
  1634. if (stat->pdata->gpio_int1 >= 0) {
  1635. INIT_WORK(&stat->irq1_work, lis2hh_acc_irq1_work_func);
  1636. stat->irq1_work_queue =
  1637. create_singlethread_workqueue("lis2hh_acc_wq1");
  1638. if (!stat->irq1_work_queue) {
  1639. err = -ENOMEM;
  1640. dev_err(&client->dev,
  1641. "cannot create work queue1: %d\n", err);
  1642. goto err_remove_hr_work_queue;
  1643. }
  1644. err = request_irq(stat->irq1, lis2hh_acc_isr1,
  1645. IRQF_TRIGGER_RISING, "lis2hh_acc_irq1", stat);
  1646. if (err < 0) {
  1647. dev_err(&client->dev, "request irq1 failed: %d\n", err);
  1648. goto err_destoyworkqueue1;
  1649. }
  1650. disable_irq_nosync(stat->irq1);
  1651. }
  1652. if (stat->pdata->gpio_int2 >= 0) {
  1653. INIT_WORK(&stat->irq2_work, lis2hh_acc_irq2_work_func);
  1654. stat->irq2_work_queue =
  1655. create_singlethread_workqueue("lis2hh_acc_wq2");
  1656. if (!stat->irq2_work_queue) {
  1657. err = -ENOMEM;
  1658. dev_err(&client->dev,
  1659. "cannot create work queue2: %d\n", err);
  1660. goto err_free_irq1;
  1661. }
  1662. err = request_irq(stat->irq2, lis2hh_acc_isr2,
  1663. IRQF_TRIGGER_RISING, "lis2hh_acc_irq2", stat);
  1664. if (err < 0) {
  1665. dev_err(&client->dev, "request irq2 failed: %d\n", err);
  1666. goto err_destoyworkqueue2;
  1667. }
  1668. disable_irq_nosync(stat->irq2);
  1669. }
  1670. mutex_unlock(&stat->lock);
  1671. dev_info(&client->dev, "%s: probed\n", LIS2HH_ACC_DEV_NAME);
  1672. return 0;
  1673. err_destoyworkqueue2:
  1674. if (stat->pdata->gpio_int2 >= 0)
  1675. destroy_workqueue(stat->irq2_work_queue);
  1676. err_free_irq1:
  1677. free_irq(stat->irq1, stat);
  1678. err_destoyworkqueue1:
  1679. if (stat->pdata->gpio_int1 >= 0)
  1680. destroy_workqueue(stat->irq1_work_queue);
  1681. err_remove_hr_work_queue:
  1682. if(stat->hr_timer_poll_work_queue) {
  1683. flush_workqueue(stat->hr_timer_poll_work_queue);
  1684. destroy_workqueue(stat->hr_timer_poll_work_queue);
  1685. }
  1686. err_power_off:
  1687. lis2hh_acc_device_power_off(stat);
  1688. err_pdata_init:
  1689. if (stat->pdata->exit)
  1690. stat->pdata->exit();
  1691. err_mutexunlock:
  1692. mutex_unlock(&stat->lock);
  1693. exit_check_functionality_failed:
  1694. pr_err("%s: Driver Init failed\n", LIS2HH_ACC_DEV_NAME);
  1695. exit:
  1696. kfree(stat);
  1697. err_free_pdata:
  1698. kfree(pdata);
  1699. return err;
  1700. }
  1701. static int __devexit lis2hh_acc_remove(struct i2c_client *client)
  1702. {
  1703. struct lis2hh_acc_status *stat = i2c_get_clientdata(client);
  1704. dev_info(&stat->client->dev, "driver removing\n");
  1705. if (stat->pdata->gpio_int1 >= 0) {
  1706. free_irq(stat->irq1, stat);
  1707. gpio_free(stat->pdata->gpio_int1);
  1708. destroy_workqueue(stat->irq1_work_queue);
  1709. }
  1710. if (stat->pdata->gpio_int2 >= 0) {
  1711. free_irq(stat->irq2, stat);
  1712. gpio_free(stat->pdata->gpio_int2);
  1713. destroy_workqueue(stat->irq2_work_queue);
  1714. }
  1715. lis2hh_acc_disable(stat);
  1716. lis2hh_acc_input_cleanup(stat);
  1717. //remove_sysfs_interfaces(&client->dev);
  1718. if(stat->hr_timer_poll_work_queue) {
  1719. flush_workqueue(stat->hr_timer_poll_work_queue);
  1720. destroy_workqueue(stat->hr_timer_poll_work_queue);
  1721. }
  1722. if (stat->pdata->exit)
  1723. stat->pdata->exit();
  1724. kfree(stat);
  1725. return 0;
  1726. }
  1727. #ifdef CONFIG_PM
  1728. static int lis2hh_acc_resume(struct i2c_client *client)
  1729. {
  1730. struct lis2hh_acc_status *stat = i2c_get_clientdata(client);
  1731. if (stat->on_before_suspend)
  1732. return lis2hh_acc_enable(stat);
  1733. return 0;
  1734. }
  1735. static int lis2hh_acc_suspend(struct i2c_client *client, pm_message_t mesg)
  1736. {
  1737. struct lis2hh_acc_status *stat = i2c_get_clientdata(client);
  1738. stat->on_before_suspend = atomic_read(&stat->enable);
  1739. return lis2hh_acc_disable(stat);
  1740. }
  1741. #else
  1742. #define lis2hh_acc_suspend NULL
  1743. #define lis2hh_acc_resume NULL
  1744. #endif /* CONFIG_PM */
  1745. #ifdef CONFIG_OF
  1746. static struct of_device_id k2hh_match_table[] = {
  1747. { .compatible = "stm,k2hh",},
  1748. {},
  1749. };
  1750. #else
  1751. #define k2hh_match_table NULL
  1752. #endif
  1753. static const struct i2c_device_id lis2hh_acc_id[]
  1754. = { { LIS2HH_ACC_DEV_NAME, 0 }, { }, };
  1755. MODULE_DEVICE_TABLE(i2c, lis2hh_acc_id);
  1756. static struct i2c_driver lis2hh_acc_driver = {
  1757. .driver = {
  1758. .owner = THIS_MODULE,
  1759. .name = LIS2HH_ACC_DEV_NAME,
  1760. .of_match_table = k2hh_match_table,
  1761. },
  1762. .probe = lis2hh_acc_probe,
  1763. .remove = __devexit_p(lis2hh_acc_remove),
  1764. .suspend = lis2hh_acc_suspend,
  1765. .resume = lis2hh_acc_resume,
  1766. .id_table = lis2hh_acc_id,
  1767. };
  1768. module_i2c_driver(lis2hh_acc_driver)
  1769. MODULE_DESCRIPTION("lis2hh accelerometer sysfs driver");
  1770. MODULE_AUTHOR("Matteo Dameno, Denis Ciocca, STMicroelectronics");
  1771. MODULE_LICENSE("GPL");