kmx61.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  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. .driver_module = THIS_MODULE,
  846. .read_raw = kmx61_read_raw,
  847. .write_raw = kmx61_write_raw,
  848. .attrs = &kmx61_acc_attribute_group,
  849. .read_event_value = kmx61_read_event,
  850. .write_event_value = kmx61_write_event,
  851. .read_event_config = kmx61_read_event_config,
  852. .write_event_config = kmx61_write_event_config,
  853. .validate_trigger = kmx61_acc_validate_trigger,
  854. };
  855. static const struct iio_info kmx61_mag_info = {
  856. .driver_module = THIS_MODULE,
  857. .read_raw = kmx61_read_raw,
  858. .write_raw = kmx61_write_raw,
  859. .attrs = &kmx61_mag_attribute_group,
  860. .validate_trigger = kmx61_mag_validate_trigger,
  861. };
  862. static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
  863. bool state)
  864. {
  865. int ret = 0;
  866. u8 device;
  867. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  868. struct kmx61_data *data = kmx61_get_data(indio_dev);
  869. mutex_lock(&data->lock);
  870. if (!state && data->ev_enable_state && data->motion_trig_on) {
  871. data->motion_trig_on = false;
  872. goto err_unlock;
  873. }
  874. if (data->acc_dready_trig == trig || data->motion_trig == trig)
  875. device = KMX61_ACC;
  876. else
  877. device = KMX61_MAG;
  878. ret = kmx61_set_power_state(data, state, device);
  879. if (ret < 0)
  880. goto err_unlock;
  881. if (data->acc_dready_trig == trig || data->mag_dready_trig == trig)
  882. ret = kmx61_setup_new_data_interrupt(data, state, device);
  883. else
  884. ret = kmx61_setup_any_motion_interrupt(data, state);
  885. if (ret < 0) {
  886. kmx61_set_power_state(data, false, device);
  887. goto err_unlock;
  888. }
  889. if (data->acc_dready_trig == trig)
  890. data->acc_dready_trig_on = state;
  891. else if (data->mag_dready_trig == trig)
  892. data->mag_dready_trig_on = state;
  893. else
  894. data->motion_trig_on = state;
  895. err_unlock:
  896. mutex_unlock(&data->lock);
  897. return ret;
  898. }
  899. static int kmx61_trig_try_reenable(struct iio_trigger *trig)
  900. {
  901. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  902. struct kmx61_data *data = kmx61_get_data(indio_dev);
  903. int ret;
  904. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
  905. if (ret < 0) {
  906. dev_err(&data->client->dev, "Error reading reg_inl\n");
  907. return ret;
  908. }
  909. return 0;
  910. }
  911. static const struct iio_trigger_ops kmx61_trigger_ops = {
  912. .set_trigger_state = kmx61_data_rdy_trigger_set_state,
  913. .try_reenable = kmx61_trig_try_reenable,
  914. .owner = THIS_MODULE,
  915. };
  916. static irqreturn_t kmx61_event_handler(int irq, void *private)
  917. {
  918. struct kmx61_data *data = private;
  919. struct iio_dev *indio_dev = data->acc_indio_dev;
  920. int ret;
  921. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1);
  922. if (ret < 0) {
  923. dev_err(&data->client->dev, "Error reading reg_ins1\n");
  924. goto ack_intr;
  925. }
  926. if (ret & KMX61_REG_INS1_BIT_WUFS) {
  927. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2);
  928. if (ret < 0) {
  929. dev_err(&data->client->dev, "Error reading reg_ins2\n");
  930. goto ack_intr;
  931. }
  932. if (ret & KMX61_REG_INS2_BIT_XN)
  933. iio_push_event(indio_dev,
  934. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  935. 0,
  936. IIO_MOD_X,
  937. IIO_EV_TYPE_THRESH,
  938. IIO_EV_DIR_FALLING),
  939. 0);
  940. if (ret & KMX61_REG_INS2_BIT_XP)
  941. iio_push_event(indio_dev,
  942. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  943. 0,
  944. IIO_MOD_X,
  945. IIO_EV_TYPE_THRESH,
  946. IIO_EV_DIR_RISING),
  947. 0);
  948. if (ret & KMX61_REG_INS2_BIT_YN)
  949. iio_push_event(indio_dev,
  950. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  951. 0,
  952. IIO_MOD_Y,
  953. IIO_EV_TYPE_THRESH,
  954. IIO_EV_DIR_FALLING),
  955. 0);
  956. if (ret & KMX61_REG_INS2_BIT_YP)
  957. iio_push_event(indio_dev,
  958. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  959. 0,
  960. IIO_MOD_Y,
  961. IIO_EV_TYPE_THRESH,
  962. IIO_EV_DIR_RISING),
  963. 0);
  964. if (ret & KMX61_REG_INS2_BIT_ZN)
  965. iio_push_event(indio_dev,
  966. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  967. 0,
  968. IIO_MOD_Z,
  969. IIO_EV_TYPE_THRESH,
  970. IIO_EV_DIR_FALLING),
  971. 0);
  972. if (ret & KMX61_REG_INS2_BIT_ZP)
  973. iio_push_event(indio_dev,
  974. IIO_MOD_EVENT_CODE(IIO_ACCEL,
  975. 0,
  976. IIO_MOD_Z,
  977. IIO_EV_TYPE_THRESH,
  978. IIO_EV_DIR_RISING),
  979. 0);
  980. }
  981. ack_intr:
  982. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
  983. if (ret < 0)
  984. dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
  985. ret |= KMX61_REG_CTRL1_BIT_RES;
  986. ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
  987. if (ret < 0)
  988. dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
  989. ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
  990. if (ret < 0)
  991. dev_err(&data->client->dev, "Error reading reg_inl\n");
  992. return IRQ_HANDLED;
  993. }
  994. static irqreturn_t kmx61_data_rdy_trig_poll(int irq, void *private)
  995. {
  996. struct kmx61_data *data = private;
  997. if (data->acc_dready_trig_on)
  998. iio_trigger_poll(data->acc_dready_trig);
  999. if (data->mag_dready_trig_on)
  1000. iio_trigger_poll(data->mag_dready_trig);
  1001. if (data->motion_trig_on)
  1002. iio_trigger_poll(data->motion_trig);
  1003. if (data->ev_enable_state)
  1004. return IRQ_WAKE_THREAD;
  1005. return IRQ_HANDLED;
  1006. }
  1007. static irqreturn_t kmx61_trigger_handler(int irq, void *p)
  1008. {
  1009. struct iio_poll_func *pf = p;
  1010. struct iio_dev *indio_dev = pf->indio_dev;
  1011. struct kmx61_data *data = kmx61_get_data(indio_dev);
  1012. int bit, ret, i = 0;
  1013. u8 base;
  1014. s16 buffer[8];
  1015. if (indio_dev == data->acc_indio_dev)
  1016. base = KMX61_ACC_XOUT_L;
  1017. else
  1018. base = KMX61_MAG_XOUT_L;
  1019. mutex_lock(&data->lock);
  1020. for_each_set_bit(bit, indio_dev->active_scan_mask,
  1021. indio_dev->masklength) {
  1022. ret = kmx61_read_measurement(data, base, bit);
  1023. if (ret < 0) {
  1024. mutex_unlock(&data->lock);
  1025. goto err;
  1026. }
  1027. buffer[i++] = ret;
  1028. }
  1029. mutex_unlock(&data->lock);
  1030. iio_push_to_buffers(indio_dev, buffer);
  1031. err:
  1032. iio_trigger_notify_done(indio_dev->trig);
  1033. return IRQ_HANDLED;
  1034. }
  1035. static const char *kmx61_match_acpi_device(struct device *dev)
  1036. {
  1037. const struct acpi_device_id *id;
  1038. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  1039. if (!id)
  1040. return NULL;
  1041. return dev_name(dev);
  1042. }
  1043. static struct iio_dev *kmx61_indiodev_setup(struct kmx61_data *data,
  1044. const struct iio_info *info,
  1045. const struct iio_chan_spec *chan,
  1046. int num_channels,
  1047. const char *name)
  1048. {
  1049. struct iio_dev *indio_dev;
  1050. indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data));
  1051. if (!indio_dev)
  1052. return ERR_PTR(-ENOMEM);
  1053. kmx61_set_data(indio_dev, data);
  1054. indio_dev->dev.parent = &data->client->dev;
  1055. indio_dev->channels = chan;
  1056. indio_dev->num_channels = num_channels;
  1057. indio_dev->name = name;
  1058. indio_dev->modes = INDIO_DIRECT_MODE;
  1059. indio_dev->info = info;
  1060. return indio_dev;
  1061. }
  1062. static struct iio_trigger *kmx61_trigger_setup(struct kmx61_data *data,
  1063. struct iio_dev *indio_dev,
  1064. const char *tag)
  1065. {
  1066. struct iio_trigger *trig;
  1067. int ret;
  1068. trig = devm_iio_trigger_alloc(&data->client->dev,
  1069. "%s-%s-dev%d",
  1070. indio_dev->name,
  1071. tag,
  1072. indio_dev->id);
  1073. if (!trig)
  1074. return ERR_PTR(-ENOMEM);
  1075. trig->dev.parent = &data->client->dev;
  1076. trig->ops = &kmx61_trigger_ops;
  1077. iio_trigger_set_drvdata(trig, indio_dev);
  1078. ret = iio_trigger_register(trig);
  1079. if (ret)
  1080. return ERR_PTR(ret);
  1081. return trig;
  1082. }
  1083. static int kmx61_probe(struct i2c_client *client,
  1084. const struct i2c_device_id *id)
  1085. {
  1086. int ret;
  1087. struct kmx61_data *data;
  1088. const char *name = NULL;
  1089. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  1090. if (!data)
  1091. return -ENOMEM;
  1092. i2c_set_clientdata(client, data);
  1093. data->client = client;
  1094. mutex_init(&data->lock);
  1095. if (id)
  1096. name = id->name;
  1097. else if (ACPI_HANDLE(&client->dev))
  1098. name = kmx61_match_acpi_device(&client->dev);
  1099. else
  1100. return -ENODEV;
  1101. data->acc_indio_dev =
  1102. kmx61_indiodev_setup(data, &kmx61_acc_info,
  1103. kmx61_acc_channels,
  1104. ARRAY_SIZE(kmx61_acc_channels),
  1105. name);
  1106. if (IS_ERR(data->acc_indio_dev))
  1107. return PTR_ERR(data->acc_indio_dev);
  1108. data->mag_indio_dev =
  1109. kmx61_indiodev_setup(data, &kmx61_mag_info,
  1110. kmx61_mag_channels,
  1111. ARRAY_SIZE(kmx61_mag_channels),
  1112. name);
  1113. if (IS_ERR(data->mag_indio_dev))
  1114. return PTR_ERR(data->mag_indio_dev);
  1115. ret = kmx61_chip_init(data);
  1116. if (ret < 0)
  1117. return ret;
  1118. if (client->irq > 0) {
  1119. ret = devm_request_threaded_irq(&client->dev, client->irq,
  1120. kmx61_data_rdy_trig_poll,
  1121. kmx61_event_handler,
  1122. IRQF_TRIGGER_RISING,
  1123. KMX61_IRQ_NAME,
  1124. data);
  1125. if (ret)
  1126. goto err_chip_uninit;
  1127. data->acc_dready_trig =
  1128. kmx61_trigger_setup(data, data->acc_indio_dev,
  1129. "dready");
  1130. if (IS_ERR(data->acc_dready_trig)) {
  1131. ret = PTR_ERR(data->acc_dready_trig);
  1132. goto err_chip_uninit;
  1133. }
  1134. data->mag_dready_trig =
  1135. kmx61_trigger_setup(data, data->mag_indio_dev,
  1136. "dready");
  1137. if (IS_ERR(data->mag_dready_trig)) {
  1138. ret = PTR_ERR(data->mag_dready_trig);
  1139. goto err_trigger_unregister_acc_dready;
  1140. }
  1141. data->motion_trig =
  1142. kmx61_trigger_setup(data, data->acc_indio_dev,
  1143. "any-motion");
  1144. if (IS_ERR(data->motion_trig)) {
  1145. ret = PTR_ERR(data->motion_trig);
  1146. goto err_trigger_unregister_mag_dready;
  1147. }
  1148. ret = iio_triggered_buffer_setup(data->acc_indio_dev,
  1149. &iio_pollfunc_store_time,
  1150. kmx61_trigger_handler,
  1151. NULL);
  1152. if (ret < 0) {
  1153. dev_err(&data->client->dev,
  1154. "Failed to setup acc triggered buffer\n");
  1155. goto err_trigger_unregister_motion;
  1156. }
  1157. ret = iio_triggered_buffer_setup(data->mag_indio_dev,
  1158. &iio_pollfunc_store_time,
  1159. kmx61_trigger_handler,
  1160. NULL);
  1161. if (ret < 0) {
  1162. dev_err(&data->client->dev,
  1163. "Failed to setup mag triggered buffer\n");
  1164. goto err_buffer_cleanup_acc;
  1165. }
  1166. }
  1167. ret = pm_runtime_set_active(&client->dev);
  1168. if (ret < 0)
  1169. goto err_buffer_cleanup_mag;
  1170. pm_runtime_enable(&client->dev);
  1171. pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
  1172. pm_runtime_use_autosuspend(&client->dev);
  1173. ret = iio_device_register(data->acc_indio_dev);
  1174. if (ret < 0) {
  1175. dev_err(&client->dev, "Failed to register acc iio device\n");
  1176. goto err_buffer_cleanup_mag;
  1177. }
  1178. ret = iio_device_register(data->mag_indio_dev);
  1179. if (ret < 0) {
  1180. dev_err(&client->dev, "Failed to register mag iio device\n");
  1181. goto err_iio_unregister_acc;
  1182. }
  1183. return 0;
  1184. err_iio_unregister_acc:
  1185. iio_device_unregister(data->acc_indio_dev);
  1186. err_buffer_cleanup_mag:
  1187. if (client->irq > 0)
  1188. iio_triggered_buffer_cleanup(data->mag_indio_dev);
  1189. err_buffer_cleanup_acc:
  1190. if (client->irq > 0)
  1191. iio_triggered_buffer_cleanup(data->acc_indio_dev);
  1192. err_trigger_unregister_motion:
  1193. iio_trigger_unregister(data->motion_trig);
  1194. err_trigger_unregister_mag_dready:
  1195. iio_trigger_unregister(data->mag_dready_trig);
  1196. err_trigger_unregister_acc_dready:
  1197. iio_trigger_unregister(data->acc_dready_trig);
  1198. err_chip_uninit:
  1199. kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
  1200. return ret;
  1201. }
  1202. static int kmx61_remove(struct i2c_client *client)
  1203. {
  1204. struct kmx61_data *data = i2c_get_clientdata(client);
  1205. iio_device_unregister(data->acc_indio_dev);
  1206. iio_device_unregister(data->mag_indio_dev);
  1207. pm_runtime_disable(&client->dev);
  1208. pm_runtime_set_suspended(&client->dev);
  1209. pm_runtime_put_noidle(&client->dev);
  1210. if (client->irq > 0) {
  1211. iio_triggered_buffer_cleanup(data->acc_indio_dev);
  1212. iio_triggered_buffer_cleanup(data->mag_indio_dev);
  1213. iio_trigger_unregister(data->acc_dready_trig);
  1214. iio_trigger_unregister(data->mag_dready_trig);
  1215. iio_trigger_unregister(data->motion_trig);
  1216. }
  1217. mutex_lock(&data->lock);
  1218. kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
  1219. mutex_unlock(&data->lock);
  1220. return 0;
  1221. }
  1222. #ifdef CONFIG_PM_SLEEP
  1223. static int kmx61_suspend(struct device *dev)
  1224. {
  1225. int ret;
  1226. struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
  1227. mutex_lock(&data->lock);
  1228. ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
  1229. false);
  1230. mutex_unlock(&data->lock);
  1231. return ret;
  1232. }
  1233. static int kmx61_resume(struct device *dev)
  1234. {
  1235. u8 stby = 0;
  1236. struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
  1237. if (data->acc_stby)
  1238. stby |= KMX61_ACC_STBY_BIT;
  1239. if (data->mag_stby)
  1240. stby |= KMX61_MAG_STBY_BIT;
  1241. return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
  1242. }
  1243. #endif
  1244. #ifdef CONFIG_PM
  1245. static int kmx61_runtime_suspend(struct device *dev)
  1246. {
  1247. struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
  1248. int ret;
  1249. mutex_lock(&data->lock);
  1250. ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
  1251. mutex_unlock(&data->lock);
  1252. return ret;
  1253. }
  1254. static int kmx61_runtime_resume(struct device *dev)
  1255. {
  1256. struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
  1257. u8 stby = 0;
  1258. if (!data->acc_ps)
  1259. stby |= KMX61_ACC_STBY_BIT;
  1260. if (!data->mag_ps)
  1261. stby |= KMX61_MAG_STBY_BIT;
  1262. return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
  1263. }
  1264. #endif
  1265. static const struct dev_pm_ops kmx61_pm_ops = {
  1266. SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
  1267. SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
  1268. };
  1269. static const struct acpi_device_id kmx61_acpi_match[] = {
  1270. {"KMX61021", 0},
  1271. {}
  1272. };
  1273. MODULE_DEVICE_TABLE(acpi, kmx61_acpi_match);
  1274. static const struct i2c_device_id kmx61_id[] = {
  1275. {"kmx611021", 0},
  1276. {}
  1277. };
  1278. MODULE_DEVICE_TABLE(i2c, kmx61_id);
  1279. static struct i2c_driver kmx61_driver = {
  1280. .driver = {
  1281. .name = KMX61_DRV_NAME,
  1282. .acpi_match_table = ACPI_PTR(kmx61_acpi_match),
  1283. .pm = &kmx61_pm_ops,
  1284. },
  1285. .probe = kmx61_probe,
  1286. .remove = kmx61_remove,
  1287. .id_table = kmx61_id,
  1288. };
  1289. module_i2c_driver(kmx61_driver);
  1290. MODULE_AUTHOR("Daniel Baluta <[email protected]>");
  1291. MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
  1292. MODULE_LICENSE("GPL v2");