ak8974.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /*
  2. * Driver for the Asahi Kasei EMD Corporation AK8974
  3. * and Aichi Steel AMI305 magnetometer chips.
  4. * Based on a patch from Samu Onkalo and the AK8975 IIO driver.
  5. *
  6. * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
  7. * Copyright (c) 2010 NVIDIA Corporation.
  8. * Copyright (C) 2016 Linaro Ltd.
  9. *
  10. * Author: Samu Onkalo <[email protected]>
  11. * Author: Linus Walleij <[email protected]>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h> /* For irq_get_irq_data() */
  18. #include <linux/completion.h>
  19. #include <linux/err.h>
  20. #include <linux/mutex.h>
  21. #include <linux/delay.h>
  22. #include <linux/bitops.h>
  23. #include <linux/regmap.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/iio/iio.h>
  27. #include <linux/iio/sysfs.h>
  28. #include <linux/iio/buffer.h>
  29. #include <linux/iio/trigger.h>
  30. #include <linux/iio/trigger_consumer.h>
  31. #include <linux/iio/triggered_buffer.h>
  32. /*
  33. * 16-bit registers are little-endian. LSB is at the address defined below
  34. * and MSB is at the next higher address.
  35. */
  36. /* These registers are common for AK8974 and AMI305 */
  37. #define AK8974_SELFTEST 0x0C
  38. #define AK8974_SELFTEST_IDLE 0x55
  39. #define AK8974_SELFTEST_OK 0xAA
  40. #define AK8974_INFO 0x0D
  41. #define AK8974_WHOAMI 0x0F
  42. #define AK8974_WHOAMI_VALUE_AMI305 0x47
  43. #define AK8974_WHOAMI_VALUE_AK8974 0x48
  44. #define AK8974_DATA_X 0x10
  45. #define AK8974_DATA_Y 0x12
  46. #define AK8974_DATA_Z 0x14
  47. #define AK8974_INT_SRC 0x16
  48. #define AK8974_STATUS 0x18
  49. #define AK8974_INT_CLEAR 0x1A
  50. #define AK8974_CTRL1 0x1B
  51. #define AK8974_CTRL2 0x1C
  52. #define AK8974_CTRL3 0x1D
  53. #define AK8974_INT_CTRL 0x1E
  54. #define AK8974_INT_THRES 0x26 /* Absolute any axis value threshold */
  55. #define AK8974_PRESET 0x30
  56. /* AK8974-specific offsets */
  57. #define AK8974_OFFSET_X 0x20
  58. #define AK8974_OFFSET_Y 0x22
  59. #define AK8974_OFFSET_Z 0x24
  60. /* AMI305-specific offsets */
  61. #define AMI305_OFFSET_X 0x6C
  62. #define AMI305_OFFSET_Y 0x72
  63. #define AMI305_OFFSET_Z 0x78
  64. /* Different temperature registers */
  65. #define AK8974_TEMP 0x31
  66. #define AMI305_TEMP 0x60
  67. #define AK8974_INT_X_HIGH BIT(7) /* Axis over +threshold */
  68. #define AK8974_INT_Y_HIGH BIT(6)
  69. #define AK8974_INT_Z_HIGH BIT(5)
  70. #define AK8974_INT_X_LOW BIT(4) /* Axis below -threshold */
  71. #define AK8974_INT_Y_LOW BIT(3)
  72. #define AK8974_INT_Z_LOW BIT(2)
  73. #define AK8974_INT_RANGE BIT(1) /* Range overflow (any axis) */
  74. #define AK8974_STATUS_DRDY BIT(6) /* Data ready */
  75. #define AK8974_STATUS_OVERRUN BIT(5) /* Data overrun */
  76. #define AK8974_STATUS_INT BIT(4) /* Interrupt occurred */
  77. #define AK8974_CTRL1_POWER BIT(7) /* 0 = standby; 1 = active */
  78. #define AK8974_CTRL1_RATE BIT(4) /* 0 = 10 Hz; 1 = 20 Hz */
  79. #define AK8974_CTRL1_FORCE_EN BIT(1) /* 0 = normal; 1 = force */
  80. #define AK8974_CTRL1_MODE2 BIT(0) /* 0 */
  81. #define AK8974_CTRL2_INT_EN BIT(4) /* 1 = enable interrupts */
  82. #define AK8974_CTRL2_DRDY_EN BIT(3) /* 1 = enable data ready signal */
  83. #define AK8974_CTRL2_DRDY_POL BIT(2) /* 1 = data ready active high */
  84. #define AK8974_CTRL2_RESDEF (AK8974_CTRL2_DRDY_POL)
  85. #define AK8974_CTRL3_RESET BIT(7) /* Software reset */
  86. #define AK8974_CTRL3_FORCE BIT(6) /* Start forced measurement */
  87. #define AK8974_CTRL3_SELFTEST BIT(4) /* Set selftest register */
  88. #define AK8974_CTRL3_RESDEF 0x00
  89. #define AK8974_INT_CTRL_XEN BIT(7) /* Enable interrupt for this axis */
  90. #define AK8974_INT_CTRL_YEN BIT(6)
  91. #define AK8974_INT_CTRL_ZEN BIT(5)
  92. #define AK8974_INT_CTRL_XYZEN (BIT(7)|BIT(6)|BIT(5))
  93. #define AK8974_INT_CTRL_POL BIT(3) /* 0 = active low; 1 = active high */
  94. #define AK8974_INT_CTRL_PULSE BIT(1) /* 0 = latched; 1 = pulse (50 usec) */
  95. #define AK8974_INT_CTRL_RESDEF (AK8974_INT_CTRL_XYZEN | AK8974_INT_CTRL_POL)
  96. /* The AMI305 has elaborate FW version and serial number registers */
  97. #define AMI305_VER 0xE8
  98. #define AMI305_SN 0xEA
  99. #define AK8974_MAX_RANGE 2048
  100. #define AK8974_POWERON_DELAY 50
  101. #define AK8974_ACTIVATE_DELAY 1
  102. #define AK8974_SELFTEST_DELAY 1
  103. /*
  104. * Set the autosuspend to two orders of magnitude larger than the poweron
  105. * delay to make sane reasonable power tradeoff savings (5 seconds in
  106. * this case).
  107. */
  108. #define AK8974_AUTOSUSPEND_DELAY 5000
  109. #define AK8974_MEASTIME 3
  110. #define AK8974_PWR_ON 1
  111. #define AK8974_PWR_OFF 0
  112. /**
  113. * struct ak8974 - state container for the AK8974 driver
  114. * @i2c: parent I2C client
  115. * @orientation: mounting matrix, flipped axis etc
  116. * @map: regmap to access the AK8974 registers over I2C
  117. * @regs: the avdd and dvdd power regulators
  118. * @name: the name of the part
  119. * @variant: the whoami ID value (for selecting code paths)
  120. * @lock: locks the magnetometer for exclusive use during a measurement
  121. * @drdy_irq: uses the DRDY IRQ line
  122. * @drdy_complete: completion for DRDY
  123. * @drdy_active_low: the DRDY IRQ is active low
  124. */
  125. struct ak8974 {
  126. struct i2c_client *i2c;
  127. struct iio_mount_matrix orientation;
  128. struct regmap *map;
  129. struct regulator_bulk_data regs[2];
  130. const char *name;
  131. u8 variant;
  132. struct mutex lock;
  133. bool drdy_irq;
  134. struct completion drdy_complete;
  135. bool drdy_active_low;
  136. };
  137. static const char ak8974_reg_avdd[] = "avdd";
  138. static const char ak8974_reg_dvdd[] = "dvdd";
  139. static int ak8974_set_power(struct ak8974 *ak8974, bool mode)
  140. {
  141. int ret;
  142. u8 val;
  143. val = mode ? AK8974_CTRL1_POWER : 0;
  144. val |= AK8974_CTRL1_FORCE_EN;
  145. ret = regmap_write(ak8974->map, AK8974_CTRL1, val);
  146. if (ret < 0)
  147. return ret;
  148. if (mode)
  149. msleep(AK8974_ACTIVATE_DELAY);
  150. return 0;
  151. }
  152. static int ak8974_reset(struct ak8974 *ak8974)
  153. {
  154. int ret;
  155. /* Power on to get register access. Sets CTRL1 reg to reset state */
  156. ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
  157. if (ret)
  158. return ret;
  159. ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_RESDEF);
  160. if (ret)
  161. return ret;
  162. ret = regmap_write(ak8974->map, AK8974_CTRL3, AK8974_CTRL3_RESDEF);
  163. if (ret)
  164. return ret;
  165. ret = regmap_write(ak8974->map, AK8974_INT_CTRL,
  166. AK8974_INT_CTRL_RESDEF);
  167. if (ret)
  168. return ret;
  169. /* After reset, power off is default state */
  170. return ak8974_set_power(ak8974, AK8974_PWR_OFF);
  171. }
  172. static int ak8974_configure(struct ak8974 *ak8974)
  173. {
  174. int ret;
  175. ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_DRDY_EN |
  176. AK8974_CTRL2_INT_EN);
  177. if (ret)
  178. return ret;
  179. ret = regmap_write(ak8974->map, AK8974_CTRL3, 0);
  180. if (ret)
  181. return ret;
  182. ret = regmap_write(ak8974->map, AK8974_INT_CTRL, AK8974_INT_CTRL_POL);
  183. if (ret)
  184. return ret;
  185. return regmap_write(ak8974->map, AK8974_PRESET, 0);
  186. }
  187. static int ak8974_trigmeas(struct ak8974 *ak8974)
  188. {
  189. unsigned int clear;
  190. u8 mask;
  191. u8 val;
  192. int ret;
  193. /* Clear any previous measurement overflow status */
  194. ret = regmap_read(ak8974->map, AK8974_INT_CLEAR, &clear);
  195. if (ret)
  196. return ret;
  197. /* If we have a DRDY IRQ line, use it */
  198. if (ak8974->drdy_irq) {
  199. mask = AK8974_CTRL2_INT_EN |
  200. AK8974_CTRL2_DRDY_EN |
  201. AK8974_CTRL2_DRDY_POL;
  202. val = AK8974_CTRL2_DRDY_EN;
  203. if (!ak8974->drdy_active_low)
  204. val |= AK8974_CTRL2_DRDY_POL;
  205. init_completion(&ak8974->drdy_complete);
  206. ret = regmap_update_bits(ak8974->map, AK8974_CTRL2,
  207. mask, val);
  208. if (ret)
  209. return ret;
  210. }
  211. /* Force a measurement */
  212. return regmap_update_bits(ak8974->map,
  213. AK8974_CTRL3,
  214. AK8974_CTRL3_FORCE,
  215. AK8974_CTRL3_FORCE);
  216. }
  217. static int ak8974_await_drdy(struct ak8974 *ak8974)
  218. {
  219. int timeout = 2;
  220. unsigned int val;
  221. int ret;
  222. if (ak8974->drdy_irq) {
  223. ret = wait_for_completion_timeout(&ak8974->drdy_complete,
  224. 1 + msecs_to_jiffies(1000));
  225. if (!ret) {
  226. dev_err(&ak8974->i2c->dev,
  227. "timeout waiting for DRDY IRQ\n");
  228. return -ETIMEDOUT;
  229. }
  230. return 0;
  231. }
  232. /* Default delay-based poll loop */
  233. do {
  234. msleep(AK8974_MEASTIME);
  235. ret = regmap_read(ak8974->map, AK8974_STATUS, &val);
  236. if (ret < 0)
  237. return ret;
  238. if (val & AK8974_STATUS_DRDY)
  239. return 0;
  240. } while (--timeout);
  241. if (!timeout) {
  242. dev_err(&ak8974->i2c->dev,
  243. "timeout waiting for DRDY\n");
  244. return -ETIMEDOUT;
  245. }
  246. return 0;
  247. }
  248. static int ak8974_getresult(struct ak8974 *ak8974, s16 *result)
  249. {
  250. unsigned int src;
  251. int ret;
  252. ret = ak8974_await_drdy(ak8974);
  253. if (ret)
  254. return ret;
  255. ret = regmap_read(ak8974->map, AK8974_INT_SRC, &src);
  256. if (ret < 0)
  257. return ret;
  258. /* Out of range overflow! Strong magnet close? */
  259. if (src & AK8974_INT_RANGE) {
  260. dev_err(&ak8974->i2c->dev,
  261. "range overflow in sensor\n");
  262. return -ERANGE;
  263. }
  264. ret = regmap_bulk_read(ak8974->map, AK8974_DATA_X, result, 6);
  265. if (ret)
  266. return ret;
  267. return ret;
  268. }
  269. static irqreturn_t ak8974_drdy_irq(int irq, void *d)
  270. {
  271. struct ak8974 *ak8974 = d;
  272. if (!ak8974->drdy_irq)
  273. return IRQ_NONE;
  274. /* TODO: timestamp here to get good measurement stamps */
  275. return IRQ_WAKE_THREAD;
  276. }
  277. static irqreturn_t ak8974_drdy_irq_thread(int irq, void *d)
  278. {
  279. struct ak8974 *ak8974 = d;
  280. unsigned int val;
  281. int ret;
  282. /* Check if this was a DRDY from us */
  283. ret = regmap_read(ak8974->map, AK8974_STATUS, &val);
  284. if (ret < 0) {
  285. dev_err(&ak8974->i2c->dev, "error reading DRDY status\n");
  286. return IRQ_HANDLED;
  287. }
  288. if (val & AK8974_STATUS_DRDY) {
  289. /* Yes this was our IRQ */
  290. complete(&ak8974->drdy_complete);
  291. return IRQ_HANDLED;
  292. }
  293. /* We may be on a shared IRQ, let the next client check */
  294. return IRQ_NONE;
  295. }
  296. static int ak8974_selftest(struct ak8974 *ak8974)
  297. {
  298. struct device *dev = &ak8974->i2c->dev;
  299. unsigned int val;
  300. int ret;
  301. ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
  302. if (ret)
  303. return ret;
  304. if (val != AK8974_SELFTEST_IDLE) {
  305. dev_err(dev, "selftest not idle before test\n");
  306. return -EIO;
  307. }
  308. /* Trigger self-test */
  309. ret = regmap_update_bits(ak8974->map,
  310. AK8974_CTRL3,
  311. AK8974_CTRL3_SELFTEST,
  312. AK8974_CTRL3_SELFTEST);
  313. if (ret) {
  314. dev_err(dev, "could not write CTRL3\n");
  315. return ret;
  316. }
  317. msleep(AK8974_SELFTEST_DELAY);
  318. ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
  319. if (ret)
  320. return ret;
  321. if (val != AK8974_SELFTEST_OK) {
  322. dev_err(dev, "selftest result NOT OK (%02x)\n", val);
  323. return -EIO;
  324. }
  325. ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
  326. if (ret)
  327. return ret;
  328. if (val != AK8974_SELFTEST_IDLE) {
  329. dev_err(dev, "selftest not idle after test (%02x)\n", val);
  330. return -EIO;
  331. }
  332. dev_dbg(dev, "passed self-test\n");
  333. return 0;
  334. }
  335. static int ak8974_get_u16_val(struct ak8974 *ak8974, u8 reg, u16 *val)
  336. {
  337. int ret;
  338. u16 bulk;
  339. ret = regmap_bulk_read(ak8974->map, reg, &bulk, 2);
  340. if (ret)
  341. return ret;
  342. *val = le16_to_cpu(bulk);
  343. return 0;
  344. }
  345. static int ak8974_detect(struct ak8974 *ak8974)
  346. {
  347. unsigned int whoami;
  348. const char *name;
  349. int ret;
  350. unsigned int fw;
  351. u16 sn;
  352. ret = regmap_read(ak8974->map, AK8974_WHOAMI, &whoami);
  353. if (ret)
  354. return ret;
  355. switch (whoami) {
  356. case AK8974_WHOAMI_VALUE_AMI305:
  357. name = "ami305";
  358. ret = regmap_read(ak8974->map, AMI305_VER, &fw);
  359. if (ret)
  360. return ret;
  361. fw &= 0x7f; /* only bits 0 thru 6 valid */
  362. ret = ak8974_get_u16_val(ak8974, AMI305_SN, &sn);
  363. if (ret)
  364. return ret;
  365. dev_info(&ak8974->i2c->dev,
  366. "detected %s, FW ver %02x, S/N: %04x\n",
  367. name, fw, sn);
  368. break;
  369. case AK8974_WHOAMI_VALUE_AK8974:
  370. name = "ak8974";
  371. dev_info(&ak8974->i2c->dev, "detected AK8974\n");
  372. break;
  373. default:
  374. dev_err(&ak8974->i2c->dev, "unsupported device (%02x) ",
  375. whoami);
  376. return -ENODEV;
  377. }
  378. ak8974->name = name;
  379. ak8974->variant = whoami;
  380. return 0;
  381. }
  382. static int ak8974_read_raw(struct iio_dev *indio_dev,
  383. struct iio_chan_spec const *chan,
  384. int *val, int *val2,
  385. long mask)
  386. {
  387. struct ak8974 *ak8974 = iio_priv(indio_dev);
  388. s16 hw_values[3];
  389. int ret = -EINVAL;
  390. pm_runtime_get_sync(&ak8974->i2c->dev);
  391. mutex_lock(&ak8974->lock);
  392. switch (mask) {
  393. case IIO_CHAN_INFO_RAW:
  394. if (chan->address > 2) {
  395. dev_err(&ak8974->i2c->dev, "faulty channel address\n");
  396. ret = -EIO;
  397. goto out_unlock;
  398. }
  399. ret = ak8974_trigmeas(ak8974);
  400. if (ret)
  401. goto out_unlock;
  402. ret = ak8974_getresult(ak8974, hw_values);
  403. if (ret)
  404. goto out_unlock;
  405. /*
  406. * We read all axes and discard all but one, for optimized
  407. * reading, use the triggered buffer.
  408. */
  409. *val = le16_to_cpu(hw_values[chan->address]);
  410. ret = IIO_VAL_INT;
  411. }
  412. out_unlock:
  413. mutex_unlock(&ak8974->lock);
  414. pm_runtime_mark_last_busy(&ak8974->i2c->dev);
  415. pm_runtime_put_autosuspend(&ak8974->i2c->dev);
  416. return ret;
  417. }
  418. static void ak8974_fill_buffer(struct iio_dev *indio_dev)
  419. {
  420. struct ak8974 *ak8974 = iio_priv(indio_dev);
  421. int ret;
  422. s16 hw_values[8]; /* Three axes + 64bit padding */
  423. pm_runtime_get_sync(&ak8974->i2c->dev);
  424. mutex_lock(&ak8974->lock);
  425. ret = ak8974_trigmeas(ak8974);
  426. if (ret) {
  427. dev_err(&ak8974->i2c->dev, "error triggering measure\n");
  428. goto out_unlock;
  429. }
  430. ret = ak8974_getresult(ak8974, hw_values);
  431. if (ret) {
  432. dev_err(&ak8974->i2c->dev, "error getting measures\n");
  433. goto out_unlock;
  434. }
  435. iio_push_to_buffers_with_timestamp(indio_dev, hw_values,
  436. iio_get_time_ns(indio_dev));
  437. out_unlock:
  438. mutex_unlock(&ak8974->lock);
  439. pm_runtime_mark_last_busy(&ak8974->i2c->dev);
  440. pm_runtime_put_autosuspend(&ak8974->i2c->dev);
  441. }
  442. static irqreturn_t ak8974_handle_trigger(int irq, void *p)
  443. {
  444. const struct iio_poll_func *pf = p;
  445. struct iio_dev *indio_dev = pf->indio_dev;
  446. ak8974_fill_buffer(indio_dev);
  447. iio_trigger_notify_done(indio_dev->trig);
  448. return IRQ_HANDLED;
  449. }
  450. static const struct iio_mount_matrix *
  451. ak8974_get_mount_matrix(const struct iio_dev *indio_dev,
  452. const struct iio_chan_spec *chan)
  453. {
  454. struct ak8974 *ak8974 = iio_priv(indio_dev);
  455. return &ak8974->orientation;
  456. }
  457. static const struct iio_chan_spec_ext_info ak8974_ext_info[] = {
  458. IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8974_get_mount_matrix),
  459. { },
  460. };
  461. #define AK8974_AXIS_CHANNEL(axis, index) \
  462. { \
  463. .type = IIO_MAGN, \
  464. .modified = 1, \
  465. .channel2 = IIO_MOD_##axis, \
  466. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  467. .ext_info = ak8974_ext_info, \
  468. .address = index, \
  469. .scan_index = index, \
  470. .scan_type = { \
  471. .sign = 's', \
  472. .realbits = 16, \
  473. .storagebits = 16, \
  474. .endianness = IIO_LE \
  475. }, \
  476. }
  477. static const struct iio_chan_spec ak8974_channels[] = {
  478. AK8974_AXIS_CHANNEL(X, 0),
  479. AK8974_AXIS_CHANNEL(Y, 1),
  480. AK8974_AXIS_CHANNEL(Z, 2),
  481. IIO_CHAN_SOFT_TIMESTAMP(3),
  482. };
  483. static const unsigned long ak8974_scan_masks[] = { 0x7, 0 };
  484. static const struct iio_info ak8974_info = {
  485. .read_raw = &ak8974_read_raw,
  486. .driver_module = THIS_MODULE,
  487. };
  488. static bool ak8974_writeable_reg(struct device *dev, unsigned int reg)
  489. {
  490. struct i2c_client *i2c = to_i2c_client(dev);
  491. struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
  492. struct ak8974 *ak8974 = iio_priv(indio_dev);
  493. switch (reg) {
  494. case AK8974_CTRL1:
  495. case AK8974_CTRL2:
  496. case AK8974_CTRL3:
  497. case AK8974_INT_CTRL:
  498. case AK8974_INT_THRES:
  499. case AK8974_INT_THRES + 1:
  500. case AK8974_PRESET:
  501. case AK8974_PRESET + 1:
  502. return true;
  503. case AK8974_OFFSET_X:
  504. case AK8974_OFFSET_X + 1:
  505. case AK8974_OFFSET_Y:
  506. case AK8974_OFFSET_Y + 1:
  507. case AK8974_OFFSET_Z:
  508. case AK8974_OFFSET_Z + 1:
  509. if (ak8974->variant == AK8974_WHOAMI_VALUE_AK8974)
  510. return true;
  511. return false;
  512. case AMI305_OFFSET_X:
  513. case AMI305_OFFSET_X + 1:
  514. case AMI305_OFFSET_Y:
  515. case AMI305_OFFSET_Y + 1:
  516. case AMI305_OFFSET_Z:
  517. case AMI305_OFFSET_Z + 1:
  518. if (ak8974->variant == AK8974_WHOAMI_VALUE_AMI305)
  519. return true;
  520. return false;
  521. default:
  522. return false;
  523. }
  524. }
  525. static const struct regmap_config ak8974_regmap_config = {
  526. .reg_bits = 8,
  527. .val_bits = 8,
  528. .max_register = 0xff,
  529. .writeable_reg = ak8974_writeable_reg,
  530. };
  531. static int ak8974_probe(struct i2c_client *i2c,
  532. const struct i2c_device_id *id)
  533. {
  534. struct iio_dev *indio_dev;
  535. struct ak8974 *ak8974;
  536. unsigned long irq_trig;
  537. int irq = i2c->irq;
  538. int ret;
  539. /* Register with IIO */
  540. indio_dev = devm_iio_device_alloc(&i2c->dev, sizeof(*ak8974));
  541. if (indio_dev == NULL)
  542. return -ENOMEM;
  543. ak8974 = iio_priv(indio_dev);
  544. i2c_set_clientdata(i2c, indio_dev);
  545. ak8974->i2c = i2c;
  546. mutex_init(&ak8974->lock);
  547. ret = of_iio_read_mount_matrix(&i2c->dev,
  548. "mount-matrix",
  549. &ak8974->orientation);
  550. if (ret)
  551. return ret;
  552. ak8974->regs[0].supply = ak8974_reg_avdd;
  553. ak8974->regs[1].supply = ak8974_reg_dvdd;
  554. ret = devm_regulator_bulk_get(&i2c->dev,
  555. ARRAY_SIZE(ak8974->regs),
  556. ak8974->regs);
  557. if (ret < 0) {
  558. dev_err(&i2c->dev, "cannot get regulators\n");
  559. return ret;
  560. }
  561. ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
  562. if (ret < 0) {
  563. dev_err(&i2c->dev, "cannot enable regulators\n");
  564. return ret;
  565. }
  566. /* Take runtime PM online */
  567. pm_runtime_get_noresume(&i2c->dev);
  568. pm_runtime_set_active(&i2c->dev);
  569. pm_runtime_enable(&i2c->dev);
  570. ak8974->map = devm_regmap_init_i2c(i2c, &ak8974_regmap_config);
  571. if (IS_ERR(ak8974->map)) {
  572. dev_err(&i2c->dev, "failed to allocate register map\n");
  573. return PTR_ERR(ak8974->map);
  574. }
  575. ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
  576. if (ret) {
  577. dev_err(&i2c->dev, "could not power on\n");
  578. goto power_off;
  579. }
  580. ret = ak8974_detect(ak8974);
  581. if (ret) {
  582. dev_err(&i2c->dev, "neither AK8974 nor AMI305 found\n");
  583. goto power_off;
  584. }
  585. ret = ak8974_selftest(ak8974);
  586. if (ret)
  587. dev_err(&i2c->dev, "selftest failed (continuing anyway)\n");
  588. ret = ak8974_reset(ak8974);
  589. if (ret) {
  590. dev_err(&i2c->dev, "AK8974 reset failed\n");
  591. goto power_off;
  592. }
  593. pm_runtime_set_autosuspend_delay(&i2c->dev,
  594. AK8974_AUTOSUSPEND_DELAY);
  595. pm_runtime_use_autosuspend(&i2c->dev);
  596. pm_runtime_put(&i2c->dev);
  597. indio_dev->dev.parent = &i2c->dev;
  598. indio_dev->channels = ak8974_channels;
  599. indio_dev->num_channels = ARRAY_SIZE(ak8974_channels);
  600. indio_dev->info = &ak8974_info;
  601. indio_dev->available_scan_masks = ak8974_scan_masks;
  602. indio_dev->modes = INDIO_DIRECT_MODE;
  603. indio_dev->name = ak8974->name;
  604. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  605. ak8974_handle_trigger,
  606. NULL);
  607. if (ret) {
  608. dev_err(&i2c->dev, "triggered buffer setup failed\n");
  609. goto disable_pm;
  610. }
  611. /* If we have a valid DRDY IRQ, make use of it */
  612. if (irq > 0) {
  613. irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
  614. if (irq_trig == IRQF_TRIGGER_RISING) {
  615. dev_info(&i2c->dev, "enable rising edge DRDY IRQ\n");
  616. } else if (irq_trig == IRQF_TRIGGER_FALLING) {
  617. ak8974->drdy_active_low = true;
  618. dev_info(&i2c->dev, "enable falling edge DRDY IRQ\n");
  619. } else {
  620. irq_trig = IRQF_TRIGGER_RISING;
  621. }
  622. irq_trig |= IRQF_ONESHOT;
  623. irq_trig |= IRQF_SHARED;
  624. ret = devm_request_threaded_irq(&i2c->dev,
  625. irq,
  626. ak8974_drdy_irq,
  627. ak8974_drdy_irq_thread,
  628. irq_trig,
  629. ak8974->name,
  630. ak8974);
  631. if (ret) {
  632. dev_err(&i2c->dev, "unable to request DRDY IRQ "
  633. "- proceeding without IRQ\n");
  634. goto no_irq;
  635. }
  636. ak8974->drdy_irq = true;
  637. }
  638. no_irq:
  639. ret = iio_device_register(indio_dev);
  640. if (ret) {
  641. dev_err(&i2c->dev, "device register failed\n");
  642. goto cleanup_buffer;
  643. }
  644. return 0;
  645. cleanup_buffer:
  646. iio_triggered_buffer_cleanup(indio_dev);
  647. disable_pm:
  648. pm_runtime_put_noidle(&i2c->dev);
  649. pm_runtime_disable(&i2c->dev);
  650. ak8974_set_power(ak8974, AK8974_PWR_OFF);
  651. power_off:
  652. regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
  653. return ret;
  654. }
  655. static int ak8974_remove(struct i2c_client *i2c)
  656. {
  657. struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
  658. struct ak8974 *ak8974 = iio_priv(indio_dev);
  659. iio_device_unregister(indio_dev);
  660. iio_triggered_buffer_cleanup(indio_dev);
  661. pm_runtime_get_sync(&i2c->dev);
  662. pm_runtime_put_noidle(&i2c->dev);
  663. pm_runtime_disable(&i2c->dev);
  664. ak8974_set_power(ak8974, AK8974_PWR_OFF);
  665. regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
  666. return 0;
  667. }
  668. static int __maybe_unused ak8974_runtime_suspend(struct device *dev)
  669. {
  670. struct ak8974 *ak8974 =
  671. iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  672. ak8974_set_power(ak8974, AK8974_PWR_OFF);
  673. regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
  674. return 0;
  675. }
  676. static int __maybe_unused ak8974_runtime_resume(struct device *dev)
  677. {
  678. struct ak8974 *ak8974 =
  679. iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  680. int ret;
  681. ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
  682. if (ret)
  683. return ret;
  684. msleep(AK8974_POWERON_DELAY);
  685. ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
  686. if (ret)
  687. goto out_regulator_disable;
  688. ret = ak8974_configure(ak8974);
  689. if (ret)
  690. goto out_disable_power;
  691. return 0;
  692. out_disable_power:
  693. ak8974_set_power(ak8974, AK8974_PWR_OFF);
  694. out_regulator_disable:
  695. regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
  696. return ret;
  697. }
  698. static const struct dev_pm_ops ak8974_dev_pm_ops = {
  699. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  700. pm_runtime_force_resume)
  701. SET_RUNTIME_PM_OPS(ak8974_runtime_suspend,
  702. ak8974_runtime_resume, NULL)
  703. };
  704. static const struct i2c_device_id ak8974_id[] = {
  705. {"ami305", 0 },
  706. {"ak8974", 0 },
  707. {}
  708. };
  709. MODULE_DEVICE_TABLE(i2c, ak8974_id);
  710. static const struct of_device_id ak8974_of_match[] = {
  711. { .compatible = "asahi-kasei,ak8974", },
  712. {}
  713. };
  714. MODULE_DEVICE_TABLE(of, ak8974_of_match);
  715. static struct i2c_driver ak8974_driver = {
  716. .driver = {
  717. .name = "ak8974",
  718. .pm = &ak8974_dev_pm_ops,
  719. .of_match_table = of_match_ptr(ak8974_of_match),
  720. },
  721. .probe = ak8974_probe,
  722. .remove = ak8974_remove,
  723. .id_table = ak8974_id,
  724. };
  725. module_i2c_driver(ak8974_driver);
  726. MODULE_DESCRIPTION("AK8974 and AMI305 3-axis magnetometer driver");
  727. MODULE_AUTHOR("Samu Onkalo");
  728. MODULE_AUTHOR("Linus Walleij");
  729. MODULE_LICENSE("GPL v2");