ak8974.c 23 KB

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