kmx61.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. /*
  2. * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
  3. *
  4. * Copyright (c) 2014, Intel Corporation.
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/i2c.h>
  15. #include <linux/acpi.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/pm.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #include <linux/iio/events.h>
  22. #include <linux/iio/trigger.h>
  23. #include <linux/iio/buffer.h>
  24. #include <linux/iio/triggered_buffer.h>
  25. #include <linux/iio/trigger_consumer.h>
  26. #define KMX61_DRV_NAME "kmx61"
  27. #define KMX61_IRQ_NAME "kmx61_event"
  28. #define KMX61_REG_WHO_AM_I 0x00
  29. #define KMX61_REG_INS1 0x01
  30. #define KMX61_REG_INS2 0x02
  31. /*
  32. * three 16-bit accelerometer output registers for X/Y/Z axis
  33. * we use only XOUT_L as a base register, all other addresses
  34. * can be obtained by applying an offset and are provided here
  35. * only for clarity.
  36. */
  37. #define KMX61_ACC_XOUT_L 0x0A
  38. #define KMX61_ACC_XOUT_H 0x0B
  39. #define KMX61_ACC_YOUT_L 0x0C
  40. #define KMX61_ACC_YOUT_H 0x0D
  41. #define KMX61_ACC_ZOUT_L 0x0E
  42. #define KMX61_ACC_ZOUT_H 0x0F
  43. /*
  44. * one 16-bit temperature output register
  45. */
  46. #define KMX61_TEMP_L 0x10
  47. #define KMX61_TEMP_H 0x11
  48. /*
  49. * three 16-bit magnetometer output registers for X/Y/Z axis
  50. */
  51. #define KMX61_MAG_XOUT_L 0x12
  52. #define KMX61_MAG_XOUT_H 0x13
  53. #define KMX61_MAG_YOUT_L 0x14
  54. #define KMX61_MAG_YOUT_H 0x15
  55. #define KMX61_MAG_ZOUT_L 0x16
  56. #define KMX61_MAG_ZOUT_H 0x17
  57. #define KMX61_REG_INL 0x28
  58. #define KMX61_REG_STBY 0x29
  59. #define KMX61_REG_CTRL1 0x2A
  60. #define KMX61_REG_CTRL2 0x2B
  61. #define KMX61_REG_ODCNTL 0x2C
  62. #define KMX61_REG_INC1 0x2D
  63. #define KMX61_REG_WUF_THRESH 0x3D
  64. #define KMX61_REG_WUF_TIMER 0x3E
  65. #define KMX61_ACC_STBY_BIT BIT(0)
  66. #define KMX61_MAG_STBY_BIT BIT(1)
  67. #define KMX61_ACT_STBY_BIT BIT(7)
  68. #define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
  69. #define KMX61_REG_INS1_BIT_WUFS BIT(1)
  70. #define KMX61_REG_INS2_BIT_ZP BIT(0)
  71. #define KMX61_REG_INS2_BIT_ZN BIT(1)
  72. #define KMX61_REG_INS2_BIT_YP BIT(2)
  73. #define KMX61_REG_INS2_BIT_YN BIT(3)
  74. #define KMX61_REG_INS2_BIT_XP BIT(4)
  75. #define KMX61_REG_INS2_BIT_XN BIT(5)
  76. #define KMX61_REG_CTRL1_GSEL_MASK 0x03
  77. #define KMX61_REG_CTRL1_BIT_RES BIT(4)
  78. #define KMX61_REG_CTRL1_BIT_DRDYE BIT(5)
  79. #define KMX61_REG_CTRL1_BIT_WUFE BIT(6)
  80. #define KMX61_REG_CTRL1_BIT_BTSE BIT(7)
  81. #define KMX61_REG_INC1_BIT_WUFS BIT(0)
  82. #define KMX61_REG_INC1_BIT_DRDYM BIT(1)
  83. #define KMX61_REG_INC1_BIT_DRDYA BIT(2)
  84. #define KMX61_REG_INC1_BIT_IEN BIT(5)
  85. #define KMX61_ACC_ODR_SHIFT 0
  86. #define KMX61_MAG_ODR_SHIFT 4
  87. #define KMX61_ACC_ODR_MASK 0x0F
  88. #define KMX61_MAG_ODR_MASK 0xF0
  89. #define KMX61_OWUF_MASK 0x7
  90. #define KMX61_DEFAULT_WAKE_THRESH 1
  91. #define KMX61_DEFAULT_WAKE_DURATION 1
  92. #define KMX61_SLEEP_DELAY_MS 2000
  93. #define KMX61_CHIP_ID 0x12
  94. /* KMX61 devices */
  95. #define KMX61_ACC 0x01
  96. #define KMX61_MAG 0x02
  97. struct kmx61_data {
  98. struct i2c_client *client;
  99. /* serialize access to non-atomic ops, e.g set_mode */
  100. struct mutex lock;
  101. /* standby state */
  102. bool acc_stby;
  103. bool mag_stby;
  104. /* power state */
  105. bool acc_ps;
  106. bool mag_ps;
  107. /* config bits */
  108. u8 range;
  109. u8 odr_bits;
  110. u8 wake_thresh;
  111. u8 wake_duration;
  112. /* accelerometer specific data */
  113. struct iio_dev *acc_indio_dev;
  114. struct iio_trigger *acc_dready_trig;
  115. struct iio_trigger *motion_trig;
  116. bool acc_dready_trig_on;
  117. bool motion_trig_on;
  118. bool ev_enable_state;
  119. /* magnetometer specific data */
  120. struct iio_dev *mag_indio_dev;
  121. struct iio_trigger *mag_dready_trig;
  122. bool mag_dready_trig_on;
  123. };
  124. enum kmx61_range {
  125. KMX61_RANGE_2G,
  126. KMX61_RANGE_4G,
  127. KMX61_RANGE_8G,
  128. };
  129. enum kmx61_axis {
  130. KMX61_AXIS_X,
  131. KMX61_AXIS_Y,
  132. KMX61_AXIS_Z,
  133. };
  134. static const u16 kmx61_uscale_table[] = {9582, 19163, 38326};
  135. static const struct {
  136. int val;
  137. int val2;
  138. } kmx61_samp_freq_table[] = { {12, 500000},
  139. {25, 0},
  140. {50, 0},
  141. {100, 0},
  142. {200, 0},
  143. {400, 0},
  144. {800, 0},
  145. {1600, 0},
  146. {0, 781000},
  147. {1, 563000},
  148. {3, 125000},
  149. {6, 250000} };
  150. static const struct {
  151. int val;
  152. int val2;
  153. int odr_bits;
  154. } kmx61_wake_up_odr_table[] = { {0, 781000, 0x00},
  155. {1, 563000, 0x01},
  156. {3, 125000, 0x02},
  157. {6, 250000, 0x03},
  158. {12, 500000, 0x04},
  159. {25, 0, 0x05},
  160. {50, 0, 0x06},
  161. {100, 0, 0x06},
  162. {200, 0, 0x06},
  163. {400, 0, 0x06},
  164. {800, 0, 0x06},
  165. {1600, 0, 0x06} };
  166. static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
  167. static IIO_CONST_ATTR(magn_scale_available, "0.001465");
  168. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
  169. "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
  170. static struct attribute *kmx61_acc_attributes[] = {
  171. &iio_const_attr_accel_scale_available.dev_attr.attr,
  172. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  173. NULL,
  174. };
  175. static struct attribute *kmx61_mag_attributes[] = {
  176. &iio_const_attr_magn_scale_available.dev_attr.attr,
  177. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  178. NULL,
  179. };
  180. static const struct attribute_group kmx61_acc_attribute_group = {
  181. .attrs = kmx61_acc_attributes,
  182. };
  183. static const struct attribute_group kmx61_mag_attribute_group = {
  184. .attrs = kmx61_mag_attributes,
  185. };
  186. static const struct iio_event_spec kmx61_event = {
  187. .type = IIO_EV_TYPE_THRESH,
  188. .dir = IIO_EV_DIR_EITHER,
  189. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  190. BIT(IIO_EV_INFO_ENABLE) |
  191. BIT(IIO_EV_INFO_PERIOD),
  192. };
  193. #define KMX61_ACC_CHAN(_axis) { \
  194. .type = IIO_ACCEL, \
  195. .modified = 1, \
  196. .channel2 = IIO_MOD_ ## _axis, \
  197. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  198. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  199. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  200. .address = KMX61_ACC, \
  201. .scan_index = KMX61_AXIS_ ## _axis, \
  202. .scan_type = { \
  203. .sign = 's', \
  204. .realbits = 12, \
  205. .storagebits = 16, \
  206. .shift = 4, \
  207. .endianness = IIO_LE, \
  208. }, \
  209. .event_spec = &kmx61_event, \
  210. .num_event_specs = 1 \
  211. }
  212. #define KMX61_MAG_CHAN(_axis) { \
  213. .type = IIO_MAGN, \
  214. .modified = 1, \
  215. .channel2 = IIO_MOD_ ## _axis, \
  216. .address = KMX61_MAG, \
  217. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  218. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  219. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  220. .scan_index = KMX61_AXIS_ ## _axis, \
  221. .scan_type = { \
  222. .sign = 's', \
  223. .realbits = 14, \
  224. .storagebits = 16, \
  225. .shift = 2, \
  226. .endianness = IIO_LE, \
  227. }, \
  228. }
  229. static const struct iio_chan_spec kmx61_acc_channels[] = {
  230. KMX61_ACC_CHAN(X),
  231. KMX61_ACC_CHAN(Y),
  232. KMX61_ACC_CHAN(Z),
  233. };
  234. static const struct iio_chan_spec kmx61_mag_channels[] = {
  235. KMX61_MAG_CHAN(X),
  236. KMX61_MAG_CHAN(Y),
  237. KMX61_MAG_CHAN(Z),
  238. };
  239. static void kmx61_set_data(struct iio_dev *indio_dev, struct kmx61_data *data)
  240. {
  241. struct kmx61_data **priv = iio_priv(indio_dev);
  242. *priv = data;
  243. }
  244. static struct kmx61_data *kmx61_get_data(struct iio_dev *indio_dev)
  245. {
  246. return *(struct kmx61_data **)iio_priv(indio_dev);
  247. }
  248. static int kmx61_convert_freq_to_bit(int val, int val2)
  249. {
  250. int i;
  251. for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
  252. if (val == kmx61_samp_freq_table[i].val &&
  253. val2 == kmx61_samp_freq_table[i].val2)
  254. return i;
  255. return -EINVAL;
  256. }
  257. static int kmx61_convert_wake_up_odr_to_bit(int val, int val2)
  258. {
  259. int i;
  260. for (i = 0; i < ARRAY_SIZE(kmx61_wake_up_odr_table); ++i)
  261. if (kmx61_wake_up_odr_table[i].val == val &&
  262. kmx61_wake_up_odr_table[i].val2 == val2)
  263. return kmx61_wake_up_odr_table[i].odr_bits;
  264. return -EINVAL;
  265. }
  266. /**
  267. * kmx61_set_mode() - set KMX61 device operating mode
  268. * @data - kmx61 device private data pointer
  269. * @mode - bitmask, indicating operating mode for @device
  270. * @device - bitmask, indicating device for which @mode needs to be set
  271. * @update - update stby bits stored in device's private @data
  272. *
  273. * For each sensor (accelerometer/magnetometer) there are two operating modes
  274. * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
  275. * if they are both enabled. Internal sensors state is saved in acc_stby and
  276. * mag_stby members of driver's private @data.
  277. */
  278. static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
  279. bool update)
  280. {
  281. int ret;
  282. int acc_stby = -1, mag_stby = -1;
  283. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
  284. if (ret < 0) {
  285. dev_err(&data->client->dev, "Error reading reg_stby\n");
  286. return ret;
  287. }
  288. if (device & KMX61_ACC) {
  289. if (mode & KMX61_ACC_STBY_BIT) {
  290. ret |= KMX61_ACC_STBY_BIT;
  291. acc_stby = 1;
  292. } else {
  293. ret &= ~KMX61_ACC_STBY_BIT;
  294. acc_stby = 0;
  295. }
  296. }
  297. if (device & KMX61_MAG) {
  298. if (mode & KMX61_MAG_STBY_BIT) {
  299. ret |= KMX61_MAG_STBY_BIT;
  300. mag_stby = 1;
  301. } else {
  302. ret &= ~KMX61_MAG_STBY_BIT;
  303. mag_stby = 0;
  304. }
  305. }
  306. if (mode & KMX61_ACT_STBY_BIT)
  307. ret |= KMX61_ACT_STBY_BIT;
  308. ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
  309. if (ret < 0) {
  310. dev_err(&data->client->dev, "Error writing reg_stby\n");
  311. return ret;
  312. }
  313. if (acc_stby != -1 && update)
  314. data->acc_stby = acc_stby;
  315. if (mag_stby != -1 && update)
  316. data->mag_stby = mag_stby;
  317. return 0;
  318. }
  319. static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
  320. {
  321. int ret;
  322. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
  323. if (ret < 0) {
  324. dev_err(&data->client->dev, "Error reading reg_stby\n");
  325. return ret;
  326. }
  327. *mode = 0;
  328. if (device & KMX61_ACC) {
  329. if (ret & KMX61_ACC_STBY_BIT)
  330. *mode |= KMX61_ACC_STBY_BIT;
  331. else
  332. *mode &= ~KMX61_ACC_STBY_BIT;
  333. }
  334. if (device & KMX61_MAG) {
  335. if (ret & KMX61_MAG_STBY_BIT)
  336. *mode |= KMX61_MAG_STBY_BIT;
  337. else
  338. *mode &= ~KMX61_MAG_STBY_BIT;
  339. }
  340. return 0;
  341. }
  342. static int kmx61_set_wake_up_odr(struct kmx61_data *data, int val, int val2)
  343. {
  344. int ret, odr_bits;
  345. odr_bits = kmx61_convert_wake_up_odr_to_bit(val, val2);
  346. if (odr_bits < 0)
  347. return odr_bits;
  348. ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL2,
  349. odr_bits);
  350. if (ret < 0)
  351. dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
  352. return ret;
  353. }
  354. static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
  355. {
  356. int ret;
  357. u8 mode;
  358. int lodr_bits, odr_bits;
  359. ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
  360. if (ret < 0)
  361. return ret;
  362. lodr_bits = kmx61_convert_freq_to_bit(val, val2);
  363. if (lodr_bits < 0)
  364. return lodr_bits;
  365. /* To change ODR, accel and magn must be in STDBY */
  366. ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
  367. true);
  368. if (ret < 0)
  369. return ret;
  370. odr_bits = 0;
  371. if (device & KMX61_ACC)
  372. odr_bits |= lodr_bits << KMX61_ACC_ODR_SHIFT;
  373. if (device & KMX61_MAG)
  374. odr_bits |= lodr_bits << KMX61_MAG_ODR_SHIFT;
  375. ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
  376. odr_bits);
  377. if (ret < 0)
  378. return ret;
  379. data->odr_bits = odr_bits;
  380. if (device & KMX61_ACC) {
  381. ret = kmx61_set_wake_up_odr(data, val, val2);
  382. if (ret)
  383. return ret;
  384. }
  385. return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
  386. }
  387. static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2,
  388. u8 device)
  389. {
  390. u8 lodr_bits;
  391. if (device & KMX61_ACC)
  392. lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
  393. KMX61_ACC_ODR_MASK;
  394. else if (device & KMX61_MAG)
  395. lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
  396. KMX61_MAG_ODR_MASK;
  397. else
  398. return -EINVAL;
  399. if (lodr_bits >= ARRAY_SIZE(kmx61_samp_freq_table))
  400. return -EINVAL;
  401. *val = kmx61_samp_freq_table[lodr_bits].val;
  402. *val2 = kmx61_samp_freq_table[lodr_bits].val2;
  403. return 0;
  404. }
  405. static int kmx61_set_range(struct kmx61_data *data, u8 range)
  406. {
  407. int ret;
  408. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
  409. if (ret < 0) {
  410. dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
  411. return ret;
  412. }
  413. ret &= ~KMX61_REG_CTRL1_GSEL_MASK;
  414. ret |= range & KMX61_REG_CTRL1_GSEL_MASK;
  415. ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
  416. if (ret < 0) {
  417. dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
  418. return ret;
  419. }
  420. data->range = range;
  421. return 0;
  422. }
  423. static int kmx61_set_scale(struct kmx61_data *data, u16 uscale)
  424. {
  425. int ret, i;
  426. u8 mode;
  427. for (i = 0; i < ARRAY_SIZE(kmx61_uscale_table); i++) {
  428. if (kmx61_uscale_table[i] == uscale) {
  429. ret = kmx61_get_mode(data, &mode,
  430. KMX61_ACC | KMX61_MAG);
  431. if (ret < 0)
  432. return ret;
  433. ret = kmx61_set_mode(data, KMX61_ALL_STBY,
  434. KMX61_ACC | KMX61_MAG, true);
  435. if (ret < 0)
  436. return ret;
  437. ret = kmx61_set_range(data, i);
  438. if (ret < 0)
  439. return ret;
  440. return kmx61_set_mode(data, mode,
  441. KMX61_ACC | KMX61_MAG, true);
  442. }
  443. }
  444. return -EINVAL;
  445. }
  446. static int kmx61_chip_init(struct kmx61_data *data)
  447. {
  448. int ret, val, val2;
  449. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
  450. if (ret < 0) {
  451. dev_err(&data->client->dev, "Error reading who_am_i\n");
  452. return ret;
  453. }
  454. if (ret != KMX61_CHIP_ID) {
  455. dev_err(&data->client->dev,
  456. "Wrong chip id, got %x expected %x\n",
  457. ret, KMX61_CHIP_ID);
  458. return -EINVAL;
  459. }
  460. /* set accel 12bit, 4g range */
  461. ret = kmx61_set_range(data, KMX61_RANGE_4G);
  462. if (ret < 0)
  463. return ret;
  464. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_ODCNTL);
  465. if (ret < 0) {
  466. dev_err(&data->client->dev, "Error reading reg_odcntl\n");
  467. return ret;
  468. }
  469. data->odr_bits = ret;
  470. /*
  471. * set output data rate for wake up (motion detection) function
  472. * to match data rate for accelerometer sampling
  473. */
  474. ret = kmx61_get_odr(data, &val, &val2, KMX61_ACC);
  475. if (ret < 0)
  476. return ret;
  477. ret = kmx61_set_wake_up_odr(data, val, val2);
  478. if (ret < 0)
  479. return ret;
  480. /* set acc/magn to OPERATION mode */
  481. ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);
  482. if (ret < 0)
  483. return ret;
  484. data->wake_thresh = KMX61_DEFAULT_WAKE_THRESH;
  485. data->wake_duration = KMX61_DEFAULT_WAKE_DURATION;
  486. return 0;
  487. }
  488. static int kmx61_setup_new_data_interrupt(struct kmx61_data *data,
  489. bool status, u8 device)
  490. {
  491. u8 mode;
  492. int ret;
  493. ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
  494. if (ret < 0)
  495. return ret;
  496. ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
  497. if (ret < 0)
  498. return ret;
  499. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
  500. if (ret < 0) {
  501. dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
  502. return ret;
  503. }
  504. if (status) {
  505. ret |= KMX61_REG_INC1_BIT_IEN;
  506. if (device & KMX61_ACC)
  507. ret |= KMX61_REG_INC1_BIT_DRDYA;
  508. if (device & KMX61_MAG)
  509. ret |= KMX61_REG_INC1_BIT_DRDYM;
  510. } else {
  511. ret &= ~KMX61_REG_INC1_BIT_IEN;
  512. if (device & KMX61_ACC)
  513. ret &= ~KMX61_REG_INC1_BIT_DRDYA;
  514. if (device & KMX61_MAG)
  515. ret &= ~KMX61_REG_INC1_BIT_DRDYM;
  516. }
  517. ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
  518. if (ret < 0) {
  519. dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
  520. return ret;
  521. }
  522. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
  523. if (ret < 0) {
  524. dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
  525. return ret;
  526. }
  527. if (status)
  528. ret |= KMX61_REG_CTRL1_BIT_DRDYE;
  529. else
  530. ret &= ~KMX61_REG_CTRL1_BIT_DRDYE;
  531. ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
  532. if (ret < 0) {
  533. dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
  534. return ret;
  535. }
  536. return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
  537. }
  538. static int kmx61_chip_update_thresholds(struct kmx61_data *data)
  539. {
  540. int ret;
  541. ret = i2c_smbus_write_byte_data(data->client,
  542. KMX61_REG_WUF_TIMER,
  543. data->wake_duration);
  544. if (ret < 0) {
  545. dev_err(&data->client->dev, "Errow writing reg_wuf_timer\n");
  546. return ret;
  547. }
  548. ret = i2c_smbus_write_byte_data(data->client,
  549. KMX61_REG_WUF_THRESH,
  550. data->wake_thresh);
  551. if (ret < 0)
  552. dev_err(&data->client->dev, "Error writing reg_wuf_thresh\n");
  553. return ret;
  554. }
  555. static int kmx61_setup_any_motion_interrupt(struct kmx61_data *data,
  556. bool status)
  557. {
  558. u8 mode;
  559. int ret;
  560. ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
  561. if (ret < 0)
  562. return ret;
  563. ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
  564. if (ret < 0)
  565. return ret;
  566. ret = kmx61_chip_update_thresholds(data);
  567. if (ret < 0)
  568. return ret;
  569. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
  570. if (ret < 0) {
  571. dev_err(&data->client->dev, "Error reading reg_inc1\n");
  572. return ret;
  573. }
  574. if (status)
  575. ret |= (KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
  576. else
  577. ret &= ~(KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
  578. ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
  579. if (ret < 0) {
  580. dev_err(&data->client->dev, "Error writing reg_inc1\n");
  581. return ret;
  582. }
  583. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
  584. if (ret < 0) {
  585. dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
  586. return ret;
  587. }
  588. if (status)
  589. ret |= KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE;
  590. else
  591. ret &= ~(KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE);
  592. ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
  593. if (ret < 0) {
  594. dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
  595. return ret;
  596. }
  597. mode |= KMX61_ACT_STBY_BIT;
  598. return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
  599. }
  600. /**
  601. * kmx61_set_power_state() - set power state for kmx61 @device
  602. * @data - kmx61 device private pointer
  603. * @on - power state to be set for @device
  604. * @device - bitmask indicating device for which @on state needs to be set
  605. *
  606. * Notice that when ACC power state needs to be set to ON and MAG is in
  607. * OPERATION then we know that kmx61_runtime_resume was already called
  608. * so we must set ACC OPERATION mode here. The same happens when MAG power
  609. * state needs to be set to ON and ACC is in OPERATION.
  610. */
  611. static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
  612. {
  613. #ifdef CONFIG_PM
  614. int ret;
  615. if (device & KMX61_ACC) {
  616. if (on && !data->acc_ps && !data->mag_stby) {
  617. ret = kmx61_set_mode(data, 0, KMX61_ACC, true);
  618. if (ret < 0)
  619. return ret;
  620. }
  621. data->acc_ps = on;
  622. }
  623. if (device & KMX61_MAG) {
  624. if (on && !data->mag_ps && !data->acc_stby) {
  625. ret = kmx61_set_mode(data, 0, KMX61_MAG, true);
  626. if (ret < 0)
  627. return ret;
  628. }
  629. data->mag_ps = on;
  630. }
  631. if (on) {
  632. ret = pm_runtime_get_sync(&data->client->dev);
  633. } else {
  634. pm_runtime_mark_last_busy(&data->client->dev);
  635. ret = pm_runtime_put_autosuspend(&data->client->dev);
  636. }
  637. if (ret < 0) {
  638. dev_err(&data->client->dev,
  639. "Failed: kmx61_set_power_state for %d, ret %d\n",
  640. on, ret);
  641. if (on)
  642. pm_runtime_put_noidle(&data->client->dev);
  643. return ret;
  644. }
  645. #endif
  646. return 0;
  647. }
  648. static int kmx61_read_measurement(struct kmx61_data *data, u8 base, u8 offset)
  649. {
  650. int ret;
  651. u8 reg = base + offset * 2;
  652. ret = i2c_smbus_read_word_data(data->client, reg);
  653. if (ret < 0)
  654. dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
  655. return ret;
  656. }
  657. static int kmx61_read_raw(struct iio_dev *indio_dev,
  658. struct iio_chan_spec const *chan, int *val,
  659. int *val2, long mask)
  660. {
  661. int ret;
  662. u8 base_reg;
  663. struct kmx61_data *data = kmx61_get_data(indio_dev);
  664. switch (mask) {
  665. case IIO_CHAN_INFO_RAW:
  666. switch (chan->type) {
  667. case IIO_ACCEL:
  668. base_reg = KMX61_ACC_XOUT_L;
  669. break;
  670. case IIO_MAGN:
  671. base_reg = KMX61_MAG_XOUT_L;
  672. break;
  673. default:
  674. return -EINVAL;
  675. }
  676. mutex_lock(&data->lock);
  677. ret = kmx61_set_power_state(data, true, chan->address);
  678. if (ret) {
  679. mutex_unlock(&data->lock);
  680. return ret;
  681. }
  682. ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
  683. if (ret < 0) {
  684. kmx61_set_power_state(data, false, chan->address);
  685. mutex_unlock(&data->lock);
  686. return ret;
  687. }
  688. *val = sign_extend32(ret >> chan->scan_type.shift,
  689. chan->scan_type.realbits - 1);
  690. ret = kmx61_set_power_state(data, false, chan->address);
  691. mutex_unlock(&data->lock);
  692. if (ret)
  693. return ret;
  694. return IIO_VAL_INT;
  695. case IIO_CHAN_INFO_SCALE:
  696. switch (chan->type) {
  697. case IIO_ACCEL:
  698. *val = 0;
  699. *val2 = kmx61_uscale_table[data->range];
  700. return IIO_VAL_INT_PLUS_MICRO;
  701. case IIO_MAGN:
  702. /* 14 bits res, 1465 microGauss per magn count */
  703. *val = 0;
  704. *val2 = 1465;
  705. return IIO_VAL_INT_PLUS_MICRO;
  706. default:
  707. return -EINVAL;
  708. }
  709. case IIO_CHAN_INFO_SAMP_FREQ:
  710. if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
  711. return -EINVAL;
  712. mutex_lock(&data->lock);
  713. ret = kmx61_get_odr(data, val, val2, chan->address);
  714. mutex_unlock(&data->lock);
  715. if (ret)
  716. return -EINVAL;
  717. return IIO_VAL_INT_PLUS_MICRO;
  718. }
  719. return -EINVAL;
  720. }
  721. static int kmx61_write_raw(struct iio_dev *indio_dev,
  722. struct iio_chan_spec const *chan, int val,
  723. int val2, long mask)
  724. {
  725. int ret;
  726. struct kmx61_data *data = kmx61_get_data(indio_dev);
  727. switch (mask) {
  728. case IIO_CHAN_INFO_SAMP_FREQ:
  729. if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
  730. return -EINVAL;
  731. mutex_lock(&data->lock);
  732. ret = kmx61_set_odr(data, val, val2, chan->address);
  733. mutex_unlock(&data->lock);
  734. return ret;
  735. case IIO_CHAN_INFO_SCALE:
  736. switch (chan->type) {
  737. case IIO_ACCEL:
  738. if (val != 0)
  739. return -EINVAL;
  740. mutex_lock(&data->lock);
  741. ret = kmx61_set_scale(data, val2);
  742. mutex_unlock(&data->lock);
  743. return ret;
  744. default:
  745. return -EINVAL;
  746. }
  747. default:
  748. return -EINVAL;
  749. }
  750. }
  751. static int kmx61_read_event(struct iio_dev *indio_dev,
  752. const struct iio_chan_spec *chan,
  753. enum iio_event_type type,
  754. enum iio_event_direction dir,
  755. enum iio_event_info info,
  756. int *val, int *val2)
  757. {
  758. struct kmx61_data *data = kmx61_get_data(indio_dev);
  759. *val2 = 0;
  760. switch (info) {
  761. case IIO_EV_INFO_VALUE:
  762. *val = data->wake_thresh;
  763. return IIO_VAL_INT;
  764. case IIO_EV_INFO_PERIOD:
  765. *val = data->wake_duration;
  766. return IIO_VAL_INT;
  767. default:
  768. return -EINVAL;
  769. }
  770. }
  771. static int kmx61_write_event(struct iio_dev *indio_dev,
  772. const struct iio_chan_spec *chan,
  773. enum iio_event_type type,
  774. enum iio_event_direction dir,
  775. enum iio_event_info info,
  776. int val, int val2)
  777. {
  778. struct kmx61_data *data = kmx61_get_data(indio_dev);
  779. if (data->ev_enable_state)
  780. return -EBUSY;
  781. switch (info) {
  782. case IIO_EV_INFO_VALUE:
  783. data->wake_thresh = val;
  784. return IIO_VAL_INT;
  785. case IIO_EV_INFO_PERIOD:
  786. data->wake_duration = val;
  787. return IIO_VAL_INT;
  788. default:
  789. return -EINVAL;
  790. }
  791. }
  792. static int kmx61_read_event_config(struct iio_dev *indio_dev,
  793. const struct iio_chan_spec *chan,
  794. enum iio_event_type type,
  795. enum iio_event_direction dir)
  796. {
  797. struct kmx61_data *data = kmx61_get_data(indio_dev);
  798. return data->ev_enable_state;
  799. }
  800. static int kmx61_write_event_config(struct iio_dev *indio_dev,
  801. const struct iio_chan_spec *chan,
  802. enum iio_event_type type,
  803. enum iio_event_direction dir,
  804. int state)
  805. {
  806. struct kmx61_data *data = kmx61_get_data(indio_dev);
  807. int ret = 0;
  808. if (state && data->ev_enable_state)
  809. return 0;
  810. mutex_lock(&data->lock);
  811. if (!state && data->motion_trig_on) {
  812. data->ev_enable_state = false;
  813. goto err_unlock;
  814. }
  815. ret = kmx61_set_power_state(data, state, KMX61_ACC);
  816. if (ret < 0)
  817. goto err_unlock;
  818. ret = kmx61_setup_any_motion_interrupt(data, state);
  819. if (ret < 0) {
  820. kmx61_set_power_state(data, false, KMX61_ACC);
  821. goto err_unlock;
  822. }
  823. data->ev_enable_state = state;
  824. err_unlock:
  825. mutex_unlock(&data->lock);
  826. return ret;
  827. }
  828. static int kmx61_acc_validate_trigger(struct iio_dev *indio_dev,
  829. struct iio_trigger *trig)
  830. {
  831. struct kmx61_data *data = kmx61_get_data(indio_dev);
  832. if (data->acc_dready_trig != trig && data->motion_trig != trig)
  833. return -EINVAL;
  834. return 0;
  835. }
  836. static int kmx61_mag_validate_trigger(struct iio_dev *indio_dev,
  837. struct iio_trigger *trig)
  838. {
  839. struct kmx61_data *data = kmx61_get_data(indio_dev);
  840. if (data->mag_dready_trig != trig)
  841. return -EINVAL;
  842. return 0;
  843. }
  844. static const struct iio_info kmx61_acc_info = {
  845. .read_raw = kmx61_read_raw,
  846. .write_raw = kmx61_write_raw,
  847. .attrs = &kmx61_acc_attribute_group,
  848. .read_event_value = kmx61_read_event,
  849. .write_event_value = kmx61_write_event,
  850. .read_event_config = kmx61_read_event_config,
  851. .write_event_config = kmx61_write_event_config,
  852. .validate_trigger = kmx61_acc_validate_trigger,
  853. };
  854. static const struct iio_info kmx61_mag_info = {
  855. .read_raw = kmx61_read_raw,
  856. .write_raw = kmx61_write_raw,
  857. .attrs = &kmx61_mag_attribute_group,
  858. .validate_trigger = kmx61_mag_validate_trigger,
  859. };
  860. static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
  861. bool state)
  862. {
  863. int ret = 0;
  864. u8 device;
  865. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  866. struct kmx61_data *data = kmx61_get_data(indio_dev);
  867. mutex_lock(&data->lock);
  868. if (!state && data->ev_enable_state && data->motion_trig_on) {
  869. data->motion_trig_on = false;
  870. goto err_unlock;
  871. }
  872. if (data->acc_dready_trig == trig || data->motion_trig == trig)
  873. device = KMX61_ACC;
  874. else
  875. device = KMX61_MAG;
  876. ret = kmx61_set_power_state(data, state, device);
  877. if (ret < 0)
  878. goto err_unlock;
  879. if (data->acc_dready_trig == trig || data->mag_dready_trig == trig)
  880. ret = kmx61_setup_new_data_interrupt(data, state, device);
  881. else
  882. ret = kmx61_setup_any_motion_interrupt(data, state);
  883. if (ret < 0) {
  884. kmx61_set_power_state(data, false, device);
  885. goto err_unlock;
  886. }
  887. if (data->acc_dready_trig == trig)
  888. data->acc_dready_trig_on = state;
  889. else if (data->mag_dready_trig == trig)
  890. data->mag_dready_trig_on = state;
  891. else
  892. data->motion_trig_on = state;
  893. err_unlock:
  894. mutex_unlock(&data->lock);
  895. return ret;
  896. }
  897. static int kmx61_trig_try_reenable(struct iio_trigger *trig)
  898. {
  899. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  900. struct kmx61_data *data = kmx61_get_data(indio_dev);
  901. int ret;
  902. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
  903. if (ret < 0) {
  904. dev_err(&data->client->dev, "Error reading reg_inl\n");
  905. return ret;
  906. }
  907. return 0;
  908. }
  909. static const struct iio_trigger_ops kmx61_trigger_ops = {
  910. .set_trigger_state = kmx61_data_rdy_trigger_set_state,
  911. .try_reenable = kmx61_trig_try_reenable,
  912. };
  913. static irqreturn_t kmx61_event_handler(int irq, void *private)
  914. {
  915. struct kmx61_data *data = private;
  916. struct iio_dev *indio_dev = data->acc_indio_dev;
  917. int ret;
  918. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1);
  919. if (ret < 0) {
  920. dev_err(&data->client->dev, "Error reading reg_ins1\n");
  921. goto ack_intr;
  922. }
  923. if (ret & KMX61_REG_INS1_BIT_WUFS) {
  924. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2);
  925. if (ret < 0) {
  926. dev_err(&data->client->dev, "Error reading reg_ins2\n");
  927. goto ack_intr;
  928. }
  929. if (ret & KMX61_REG_INS2_BIT_XN)
  930. iio_push_event(indio_dev,
  931. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  932. 0,
  933. IIO_MOD_X,
  934. IIO_EV_TYPE_THRESH,
  935. IIO_EV_DIR_FALLING),
  936. 0);
  937. if (ret & KMX61_REG_INS2_BIT_XP)
  938. iio_push_event(indio_dev,
  939. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  940. 0,
  941. IIO_MOD_X,
  942. IIO_EV_TYPE_THRESH,
  943. IIO_EV_DIR_RISING),
  944. 0);
  945. if (ret & KMX61_REG_INS2_BIT_YN)
  946. iio_push_event(indio_dev,
  947. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  948. 0,
  949. IIO_MOD_Y,
  950. IIO_EV_TYPE_THRESH,
  951. IIO_EV_DIR_FALLING),
  952. 0);
  953. if (ret & KMX61_REG_INS2_BIT_YP)
  954. iio_push_event(indio_dev,
  955. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  956. 0,
  957. IIO_MOD_Y,
  958. IIO_EV_TYPE_THRESH,
  959. IIO_EV_DIR_RISING),
  960. 0);
  961. if (ret & KMX61_REG_INS2_BIT_ZN)
  962. iio_push_event(indio_dev,
  963. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  964. 0,
  965. IIO_MOD_Z,
  966. IIO_EV_TYPE_THRESH,
  967. IIO_EV_DIR_FALLING),
  968. 0);
  969. if (ret & KMX61_REG_INS2_BIT_ZP)
  970. iio_push_event(indio_dev,
  971. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  972. 0,
  973. IIO_MOD_Z,
  974. IIO_EV_TYPE_THRESH,
  975. IIO_EV_DIR_RISING),
  976. 0);
  977. }
  978. ack_intr:
  979. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
  980. if (ret < 0)
  981. dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
  982. ret |= KMX61_REG_CTRL1_BIT_RES;
  983. ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
  984. if (ret < 0)
  985. dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
  986. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
  987. if (ret < 0)
  988. dev_err(&data->client->dev, "Error reading reg_inl\n");
  989. return IRQ_HANDLED;
  990. }
  991. static irqreturn_t kmx61_data_rdy_trig_poll(int irq, void *private)
  992. {
  993. struct kmx61_data *data = private;
  994. if (data->acc_dready_trig_on)
  995. iio_trigger_poll(data->acc_dready_trig);
  996. if (data->mag_dready_trig_on)
  997. iio_trigger_poll(data->mag_dready_trig);
  998. if (data->motion_trig_on)
  999. iio_trigger_poll(data->motion_trig);
  1000. if (data->ev_enable_state)
  1001. return IRQ_WAKE_THREAD;
  1002. return IRQ_HANDLED;
  1003. }
  1004. static irqreturn_t kmx61_trigger_handler(int irq, void *p)
  1005. {
  1006. struct iio_poll_func *pf = p;
  1007. struct iio_dev *indio_dev = pf->indio_dev;
  1008. struct kmx61_data *data = kmx61_get_data(indio_dev);
  1009. int bit, ret, i = 0;
  1010. u8 base;
  1011. s16 buffer[8];
  1012. if (indio_dev == data->acc_indio_dev)
  1013. base = KMX61_ACC_XOUT_L;
  1014. else
  1015. base = KMX61_MAG_XOUT_L;
  1016. mutex_lock(&data->lock);
  1017. for_each_set_bit(bit, indio_dev->active_scan_mask,
  1018. indio_dev->masklength) {
  1019. ret = kmx61_read_measurement(data, base, bit);
  1020. if (ret < 0) {
  1021. mutex_unlock(&data->lock);
  1022. goto err;
  1023. }
  1024. buffer[i++] = ret;
  1025. }
  1026. mutex_unlock(&data->lock);
  1027. iio_push_to_buffers(indio_dev, buffer);
  1028. err:
  1029. iio_trigger_notify_done(indio_dev->trig);
  1030. return IRQ_HANDLED;
  1031. }
  1032. static const char *kmx61_match_acpi_device(struct device *dev)
  1033. {
  1034. const struct acpi_device_id *id;
  1035. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  1036. if (!id)
  1037. return NULL;
  1038. return dev_name(dev);
  1039. }
  1040. static struct iio_dev *kmx61_indiodev_setup(struct kmx61_data *data,
  1041. const struct iio_info *info,
  1042. const struct iio_chan_spec *chan,
  1043. int num_channels,
  1044. const char *name)
  1045. {
  1046. struct iio_dev *indio_dev;
  1047. indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data));
  1048. if (!indio_dev)
  1049. return ERR_PTR(-ENOMEM);
  1050. kmx61_set_data(indio_dev, data);
  1051. indio_dev->dev.parent = &data->client->dev;
  1052. indio_dev->channels = chan;
  1053. indio_dev->num_channels = num_channels;
  1054. indio_dev->name = name;
  1055. indio_dev->modes = INDIO_DIRECT_MODE;
  1056. indio_dev->info = info;
  1057. return indio_dev;
  1058. }
  1059. static struct iio_trigger *kmx61_trigger_setup(struct kmx61_data *data,
  1060. struct iio_dev *indio_dev,
  1061. const char *tag)
  1062. {
  1063. struct iio_trigger *trig;
  1064. int ret;
  1065. trig = devm_iio_trigger_alloc(&data->client->dev,
  1066. "%s-%s-dev%d",
  1067. indio_dev->name,
  1068. tag,
  1069. indio_dev->id);
  1070. if (!trig)
  1071. return ERR_PTR(-ENOMEM);
  1072. trig->dev.parent = &data->client->dev;
  1073. trig->ops = &kmx61_trigger_ops;
  1074. iio_trigger_set_drvdata(trig, indio_dev);
  1075. ret = iio_trigger_register(trig);
  1076. if (ret)
  1077. return ERR_PTR(ret);
  1078. return trig;
  1079. }
  1080. static int kmx61_probe(struct i2c_client *client,
  1081. const struct i2c_device_id *id)
  1082. {
  1083. int ret;
  1084. struct kmx61_data *data;
  1085. const char *name = NULL;
  1086. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  1087. if (!data)
  1088. return -ENOMEM;
  1089. i2c_set_clientdata(client, data);
  1090. data->client = client;
  1091. mutex_init(&data->lock);
  1092. if (id)
  1093. name = id->name;
  1094. else if (ACPI_HANDLE(&client->dev))
  1095. name = kmx61_match_acpi_device(&client->dev);
  1096. else
  1097. return -ENODEV;
  1098. data->acc_indio_dev =
  1099. kmx61_indiodev_setup(data, &kmx61_acc_info,
  1100. kmx61_acc_channels,
  1101. ARRAY_SIZE(kmx61_acc_channels),
  1102. name);
  1103. if (IS_ERR(data->acc_indio_dev))
  1104. return PTR_ERR(data->acc_indio_dev);
  1105. data->mag_indio_dev =
  1106. kmx61_indiodev_setup(data, &kmx61_mag_info,
  1107. kmx61_mag_channels,
  1108. ARRAY_SIZE(kmx61_mag_channels),
  1109. name);
  1110. if (IS_ERR(data->mag_indio_dev))
  1111. return PTR_ERR(data->mag_indio_dev);
  1112. ret = kmx61_chip_init(data);
  1113. if (ret < 0)
  1114. return ret;
  1115. if (client->irq > 0) {
  1116. ret = devm_request_threaded_irq(&client->dev, client->irq,
  1117. kmx61_data_rdy_trig_poll,
  1118. kmx61_event_handler,
  1119. IRQF_TRIGGER_RISING,
  1120. KMX61_IRQ_NAME,
  1121. data);
  1122. if (ret)
  1123. goto err_chip_uninit;
  1124. data->acc_dready_trig =
  1125. kmx61_trigger_setup(data, data->acc_indio_dev,
  1126. "dready");
  1127. if (IS_ERR(data->acc_dready_trig)) {
  1128. ret = PTR_ERR(data->acc_dready_trig);
  1129. goto err_chip_uninit;
  1130. }
  1131. data->mag_dready_trig =
  1132. kmx61_trigger_setup(data, data->mag_indio_dev,
  1133. "dready");
  1134. if (IS_ERR(data->mag_dready_trig)) {
  1135. ret = PTR_ERR(data->mag_dready_trig);
  1136. goto err_trigger_unregister_acc_dready;
  1137. }
  1138. data->motion_trig =
  1139. kmx61_trigger_setup(data, data->acc_indio_dev,
  1140. "any-motion");
  1141. if (IS_ERR(data->motion_trig)) {
  1142. ret = PTR_ERR(data->motion_trig);
  1143. goto err_trigger_unregister_mag_dready;
  1144. }
  1145. ret = iio_triggered_buffer_setup(data->acc_indio_dev,
  1146. &iio_pollfunc_store_time,
  1147. kmx61_trigger_handler,
  1148. NULL);
  1149. if (ret < 0) {
  1150. dev_err(&data->client->dev,
  1151. "Failed to setup acc triggered buffer\n");
  1152. goto err_trigger_unregister_motion;
  1153. }
  1154. ret = iio_triggered_buffer_setup(data->mag_indio_dev,
  1155. &iio_pollfunc_store_time,
  1156. kmx61_trigger_handler,
  1157. NULL);
  1158. if (ret < 0) {
  1159. dev_err(&data->client->dev,
  1160. "Failed to setup mag triggered buffer\n");
  1161. goto err_buffer_cleanup_acc;
  1162. }
  1163. }
  1164. ret = pm_runtime_set_active(&client->dev);
  1165. if (ret < 0)
  1166. goto err_buffer_cleanup_mag;
  1167. pm_runtime_enable(&client->dev);
  1168. pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
  1169. pm_runtime_use_autosuspend(&client->dev);
  1170. ret = iio_device_register(data->acc_indio_dev);
  1171. if (ret < 0) {
  1172. dev_err(&client->dev, "Failed to register acc iio device\n");
  1173. goto err_buffer_cleanup_mag;
  1174. }
  1175. ret = iio_device_register(data->mag_indio_dev);
  1176. if (ret < 0) {
  1177. dev_err(&client->dev, "Failed to register mag iio device\n");
  1178. goto err_iio_unregister_acc;
  1179. }
  1180. return 0;
  1181. err_iio_unregister_acc:
  1182. iio_device_unregister(data->acc_indio_dev);
  1183. err_buffer_cleanup_mag:
  1184. if (client->irq > 0)
  1185. iio_triggered_buffer_cleanup(data->mag_indio_dev);
  1186. err_buffer_cleanup_acc:
  1187. if (client->irq > 0)
  1188. iio_triggered_buffer_cleanup(data->acc_indio_dev);
  1189. err_trigger_unregister_motion:
  1190. iio_trigger_unregister(data->motion_trig);
  1191. err_trigger_unregister_mag_dready:
  1192. iio_trigger_unregister(data->mag_dready_trig);
  1193. err_trigger_unregister_acc_dready:
  1194. iio_trigger_unregister(data->acc_dready_trig);
  1195. err_chip_uninit:
  1196. kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
  1197. return ret;
  1198. }
  1199. static int kmx61_remove(struct i2c_client *client)
  1200. {
  1201. struct kmx61_data *data = i2c_get_clientdata(client);
  1202. iio_device_unregister(data->acc_indio_dev);
  1203. iio_device_unregister(data->mag_indio_dev);
  1204. pm_runtime_disable(&client->dev);
  1205. pm_runtime_set_suspended(&client->dev);
  1206. pm_runtime_put_noidle(&client->dev);
  1207. if (client->irq > 0) {
  1208. iio_triggered_buffer_cleanup(data->acc_indio_dev);
  1209. iio_triggered_buffer_cleanup(data->mag_indio_dev);
  1210. iio_trigger_unregister(data->acc_dready_trig);
  1211. iio_trigger_unregister(data->mag_dready_trig);
  1212. iio_trigger_unregister(data->motion_trig);
  1213. }
  1214. mutex_lock(&data->lock);
  1215. kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
  1216. mutex_unlock(&data->lock);
  1217. return 0;
  1218. }
  1219. #ifdef CONFIG_PM_SLEEP
  1220. static int kmx61_suspend(struct device *dev)
  1221. {
  1222. int ret;
  1223. struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
  1224. mutex_lock(&data->lock);
  1225. ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
  1226. false);
  1227. mutex_unlock(&data->lock);
  1228. return ret;
  1229. }
  1230. static int kmx61_resume(struct device *dev)
  1231. {
  1232. u8 stby = 0;
  1233. struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
  1234. if (data->acc_stby)
  1235. stby |= KMX61_ACC_STBY_BIT;
  1236. if (data->mag_stby)
  1237. stby |= KMX61_MAG_STBY_BIT;
  1238. return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
  1239. }
  1240. #endif
  1241. #ifdef CONFIG_PM
  1242. static int kmx61_runtime_suspend(struct device *dev)
  1243. {
  1244. struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
  1245. int ret;
  1246. mutex_lock(&data->lock);
  1247. ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
  1248. mutex_unlock(&data->lock);
  1249. return ret;
  1250. }
  1251. static int kmx61_runtime_resume(struct device *dev)
  1252. {
  1253. struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
  1254. u8 stby = 0;
  1255. if (!data->acc_ps)
  1256. stby |= KMX61_ACC_STBY_BIT;
  1257. if (!data->mag_ps)
  1258. stby |= KMX61_MAG_STBY_BIT;
  1259. return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
  1260. }
  1261. #endif
  1262. static const struct dev_pm_ops kmx61_pm_ops = {
  1263. SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
  1264. SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
  1265. };
  1266. static const struct acpi_device_id kmx61_acpi_match[] = {
  1267. {"KMX61021", 0},
  1268. {}
  1269. };
  1270. MODULE_DEVICE_TABLE(acpi, kmx61_acpi_match);
  1271. static const struct i2c_device_id kmx61_id[] = {
  1272. {"kmx611021", 0},
  1273. {}
  1274. };
  1275. MODULE_DEVICE_TABLE(i2c, kmx61_id);
  1276. static struct i2c_driver kmx61_driver = {
  1277. .driver = {
  1278. .name = KMX61_DRV_NAME,
  1279. .acpi_match_table = ACPI_PTR(kmx61_acpi_match),
  1280. .pm = &kmx61_pm_ops,
  1281. },
  1282. .probe = kmx61_probe,
  1283. .remove = kmx61_remove,
  1284. .id_table = kmx61_id,
  1285. };
  1286. module_i2c_driver(kmx61_driver);
  1287. MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
  1288. MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
  1289. MODULE_LICENSE("GPL v2");