yamaha-yas530.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for the Yamaha YAS magnetic sensors, often used in Samsung
  4. * mobile phones. While all are not yet handled because of lacking
  5. * hardware, expand this driver to handle the different variants:
  6. *
  7. * YAS530 MS-3E (2011 Samsung Galaxy S Advance)
  8. * YAS532 MS-3R (2011 Samsung Galaxy S4)
  9. * YAS533 MS-3F (Vivo 1633, 1707, V3, Y21L)
  10. * (YAS534 is a magnetic switch, not handled)
  11. * YAS535 MS-6C
  12. * YAS536 MS-3W
  13. * YAS537 MS-3T (2015 Samsung Galaxy S6, Note 5, Galaxy S7)
  14. * YAS539 MS-3S (2018 Samsung Galaxy A7 SM-A750FN)
  15. *
  16. * Code functions found in the MPU3050 YAS530 and YAS532 drivers
  17. * named "inv_compass" in the Tegra Android kernel tree.
  18. * Copyright (C) 2012 InvenSense Corporation
  19. *
  20. * Code functions for YAS537 based on Yamaha Android kernel driver.
  21. * Copyright (c) 2014 Yamaha Corporation
  22. *
  23. * Author: Linus Walleij <[email protected]>
  24. */
  25. #include <linux/bitfield.h>
  26. #include <linux/bitops.h>
  27. #include <linux/delay.h>
  28. #include <linux/err.h>
  29. #include <linux/gpio/consumer.h>
  30. #include <linux/i2c.h>
  31. #include <linux/module.h>
  32. #include <linux/mod_devicetable.h>
  33. #include <linux/mutex.h>
  34. #include <linux/pm_runtime.h>
  35. #include <linux/property.h>
  36. #include <linux/regmap.h>
  37. #include <linux/regulator/consumer.h>
  38. #include <linux/random.h>
  39. #include <linux/units.h>
  40. #include <linux/iio/buffer.h>
  41. #include <linux/iio/iio.h>
  42. #include <linux/iio/trigger_consumer.h>
  43. #include <linux/iio/triggered_buffer.h>
  44. #include <asm/unaligned.h>
  45. /* Commonly used registers */
  46. #define YAS5XX_DEVICE_ID 0x80
  47. #define YAS5XX_MEASURE_DATA 0xB0
  48. /* These registers are used by YAS530, YAS532 and YAS533 */
  49. #define YAS530_ACTUATE_INIT_COIL 0x81
  50. #define YAS530_MEASURE 0x82
  51. #define YAS530_CONFIG 0x83
  52. #define YAS530_MEASURE_INTERVAL 0x84
  53. #define YAS530_OFFSET_X 0x85 /* [-31 .. 31] */
  54. #define YAS530_OFFSET_Y1 0x86 /* [-31 .. 31] */
  55. #define YAS530_OFFSET_Y2 0x87 /* [-31 .. 31] */
  56. #define YAS530_TEST1 0x88
  57. #define YAS530_TEST2 0x89
  58. #define YAS530_CAL 0x90
  59. /* Registers used by YAS537 */
  60. #define YAS537_MEASURE 0x81 /* Originally YAS537_REG_CMDR */
  61. #define YAS537_CONFIG 0x82 /* Originally YAS537_REG_CONFR */
  62. #define YAS537_MEASURE_INTERVAL 0x83 /* Originally YAS537_REG_INTRVLR */
  63. #define YAS537_OFFSET_X 0x84 /* Originally YAS537_REG_OXR */
  64. #define YAS537_OFFSET_Y1 0x85 /* Originally YAS537_REG_OY1R */
  65. #define YAS537_OFFSET_Y2 0x86 /* Originally YAS537_REG_OY2R */
  66. #define YAS537_AVR 0x87
  67. #define YAS537_HCK 0x88
  68. #define YAS537_LCK 0x89
  69. #define YAS537_SRST 0x90
  70. #define YAS537_ADCCAL 0x91
  71. #define YAS537_MTC 0x93
  72. #define YAS537_OC 0x9E
  73. #define YAS537_TRM 0x9F
  74. #define YAS537_CAL 0xC0
  75. /* Bits in the YAS5xx config register */
  76. #define YAS5XX_CONFIG_INTON BIT(0) /* Interrupt on? */
  77. #define YAS5XX_CONFIG_INTHACT BIT(1) /* Interrupt active high? */
  78. #define YAS5XX_CONFIG_CCK_MASK GENMASK(4, 2)
  79. #define YAS5XX_CONFIG_CCK_SHIFT 2
  80. /* Bits in the measure command register */
  81. #define YAS5XX_MEASURE_START BIT(0)
  82. #define YAS5XX_MEASURE_LDTC BIT(1)
  83. #define YAS5XX_MEASURE_FORS BIT(2)
  84. #define YAS5XX_MEASURE_DLYMES BIT(4)
  85. #define YAS5XX_MEASURE_CONT BIT(5)
  86. /* Bits in the measure data register */
  87. #define YAS5XX_MEASURE_DATA_BUSY BIT(7)
  88. #define YAS530_DEVICE_ID 0x01 /* YAS530 (MS-3E) */
  89. #define YAS530_VERSION_A 0 /* YAS530 (MS-3E A) */
  90. #define YAS530_VERSION_B 1 /* YAS530B (MS-3E B) */
  91. #define YAS530_VERSION_A_COEF 380
  92. #define YAS530_VERSION_B_COEF 550
  93. #define YAS530_DATA_BITS 12
  94. #define YAS530_DATA_CENTER BIT(YAS530_DATA_BITS - 1)
  95. #define YAS530_DATA_OVERFLOW (BIT(YAS530_DATA_BITS) - 1)
  96. #define YAS532_DEVICE_ID 0x02 /* YAS532/YAS533 (MS-3R/F) */
  97. #define YAS532_VERSION_AB 0 /* YAS532/533 AB (MS-3R/F AB) */
  98. #define YAS532_VERSION_AC 1 /* YAS532/533 AC (MS-3R/F AC) */
  99. #define YAS532_VERSION_AB_COEF 1800
  100. #define YAS532_VERSION_AC_COEF_X 850
  101. #define YAS532_VERSION_AC_COEF_Y1 750
  102. #define YAS532_VERSION_AC_COEF_Y2 750
  103. #define YAS532_DATA_BITS 13
  104. #define YAS532_DATA_CENTER BIT(YAS532_DATA_BITS - 1)
  105. #define YAS532_DATA_OVERFLOW (BIT(YAS532_DATA_BITS) - 1)
  106. #define YAS537_DEVICE_ID 0x07 /* YAS537 (MS-3T) */
  107. #define YAS537_VERSION_0 0 /* Version naming unknown */
  108. #define YAS537_VERSION_1 1 /* Version naming unknown */
  109. #define YAS537_MAG_AVERAGE_32_MASK GENMASK(6, 4)
  110. #define YAS537_MEASURE_TIME_WORST_US 1500
  111. #define YAS537_DEFAULT_SENSOR_DELAY_MS 50
  112. #define YAS537_MAG_RCOIL_TIME_US 65
  113. #define YAS537_MTC3_MASK_PREP GENMASK(7, 0)
  114. #define YAS537_MTC3_MASK_GET GENMASK(7, 5)
  115. #define YAS537_MTC3_ADD_BIT BIT(4)
  116. #define YAS537_HCK_MASK_PREP GENMASK(4, 0)
  117. #define YAS537_HCK_MASK_GET GENMASK(7, 4)
  118. #define YAS537_LCK_MASK_PREP GENMASK(4, 0)
  119. #define YAS537_LCK_MASK_GET GENMASK(3, 0)
  120. #define YAS537_OC_MASK_GET GENMASK(5, 0)
  121. /* Turn off device regulators etc after 5 seconds of inactivity */
  122. #define YAS5XX_AUTOSUSPEND_DELAY_MS 5000
  123. enum chip_ids {
  124. yas530,
  125. yas532,
  126. yas533,
  127. yas537,
  128. };
  129. static const int yas530_volatile_reg[] = {
  130. YAS530_ACTUATE_INIT_COIL,
  131. YAS530_MEASURE,
  132. };
  133. static const int yas537_volatile_reg[] = {
  134. YAS537_MEASURE,
  135. };
  136. struct yas5xx_calibration {
  137. /* Linearization calibration x, y1, y2 */
  138. s32 r[3];
  139. u32 f[3];
  140. /* Temperature compensation calibration */
  141. s16 Cx, Cy1, Cy2;
  142. /* Misc calibration coefficients */
  143. s8 a2, a3, a4, a6, a7, a8;
  144. s16 a5, a9;
  145. u8 k;
  146. /* clock divider */
  147. u8 dck;
  148. };
  149. struct yas5xx;
  150. /**
  151. * struct yas5xx_chip_info - device-specific data and function pointers
  152. * @devid: device ID number
  153. * @product_name: product name of the YAS variant
  154. * @version_names: version letters or namings
  155. * @volatile_reg: device-specific volatile registers
  156. * @volatile_reg_qty: quantity of device-specific volatile registers
  157. * @scaling_val2: scaling value for IIO_CHAN_INFO_SCALE
  158. * @t_ref: number of counts at reference temperature 20 °C
  159. * @min_temp_x10: starting point of temperature counting in 1/10:s degrees Celsius
  160. * @get_measure: function pointer to get a measurement
  161. * @get_calibration_data: function pointer to get calibration data
  162. * @dump_calibration: function pointer to dump calibration for debugging
  163. * @measure_offsets: function pointer to measure the offsets
  164. * @power_on: function pointer to power-on procedure
  165. *
  166. * The "t_ref" value for YAS532/533 is known from the Android driver.
  167. * For YAS530 and YAS537 it was approximately measured.
  168. *
  169. * The temperatures "min_temp_x10" are derived from the temperature resolutions
  170. * given in the data sheets.
  171. */
  172. struct yas5xx_chip_info {
  173. unsigned int devid;
  174. const char *product_name;
  175. const char *version_names[2];
  176. const int *volatile_reg;
  177. int volatile_reg_qty;
  178. u32 scaling_val2;
  179. u16 t_ref;
  180. s16 min_temp_x10;
  181. int (*get_measure)(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo);
  182. int (*get_calibration_data)(struct yas5xx *yas5xx);
  183. void (*dump_calibration)(struct yas5xx *yas5xx);
  184. int (*measure_offsets)(struct yas5xx *yas5xx);
  185. int (*power_on)(struct yas5xx *yas5xx);
  186. };
  187. /**
  188. * struct yas5xx - state container for the YAS5xx driver
  189. * @dev: parent device pointer
  190. * @chip_info: device-specific data and function pointers
  191. * @version: device version
  192. * @calibration: calibration settings from the OTP storage
  193. * @hard_offsets: offsets for each axis measured with initcoil actuated
  194. * @orientation: mounting matrix, flipped axis etc
  195. * @map: regmap to access the YAX5xx registers over I2C
  196. * @regs: the vdd and vddio power regulators
  197. * @reset: optional GPIO line used for handling RESET
  198. * @lock: locks the magnetometer for exclusive use during a measurement (which
  199. * involves several register transactions so the regmap lock is not enough)
  200. * so that measurements get serialized in a first-come-first serve manner
  201. * @scan: naturally aligned measurements
  202. */
  203. struct yas5xx {
  204. struct device *dev;
  205. const struct yas5xx_chip_info *chip_info;
  206. unsigned int version;
  207. struct yas5xx_calibration calibration;
  208. s8 hard_offsets[3];
  209. struct iio_mount_matrix orientation;
  210. struct regmap *map;
  211. struct regulator_bulk_data regs[2];
  212. struct gpio_desc *reset;
  213. struct mutex lock;
  214. /*
  215. * The scanout is 4 x 32 bits in CPU endianness.
  216. * Ensure timestamp is naturally aligned
  217. */
  218. struct {
  219. s32 channels[4];
  220. s64 ts __aligned(8);
  221. } scan;
  222. };
  223. /* On YAS530 the x, y1 and y2 values are 12 bits */
  224. static u16 yas530_extract_axis(u8 *data)
  225. {
  226. u16 val;
  227. /*
  228. * These are the bits used in a 16bit word:
  229. * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  230. * x x x x x x x x x x x x
  231. */
  232. val = get_unaligned_be16(&data[0]);
  233. val = FIELD_GET(GENMASK(14, 3), val);
  234. return val;
  235. }
  236. /* On YAS532 the x, y1 and y2 values are 13 bits */
  237. static u16 yas532_extract_axis(u8 *data)
  238. {
  239. u16 val;
  240. /*
  241. * These are the bits used in a 16bit word:
  242. * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  243. * x x x x x x x x x x x x x
  244. */
  245. val = get_unaligned_be16(&data[0]);
  246. val = FIELD_GET(GENMASK(14, 2), val);
  247. return val;
  248. }
  249. /**
  250. * yas530_measure() - Make a measure from the hardware
  251. * @yas5xx: The device state
  252. * @t: the raw temperature measurement
  253. * @x: the raw x axis measurement
  254. * @y1: the y1 axis measurement
  255. * @y2: the y2 axis measurement
  256. * @return: 0 on success or error code
  257. *
  258. * Used by YAS530, YAS532 and YAS533.
  259. */
  260. static int yas530_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y2)
  261. {
  262. const struct yas5xx_chip_info *ci = yas5xx->chip_info;
  263. unsigned int busy;
  264. u8 data[8];
  265. int ret;
  266. u16 val;
  267. mutex_lock(&yas5xx->lock);
  268. ret = regmap_write(yas5xx->map, YAS530_MEASURE, YAS5XX_MEASURE_START);
  269. if (ret < 0)
  270. goto out_unlock;
  271. /*
  272. * Typical time to measure 1500 us, max 2000 us so wait min 500 us
  273. * and at most 20000 us (one magnitude more than the datsheet max)
  274. * before timeout.
  275. */
  276. ret = regmap_read_poll_timeout(yas5xx->map, YAS5XX_MEASURE_DATA, busy,
  277. !(busy & YAS5XX_MEASURE_DATA_BUSY),
  278. 500, 20000);
  279. if (ret) {
  280. dev_err(yas5xx->dev, "timeout waiting for measurement\n");
  281. goto out_unlock;
  282. }
  283. ret = regmap_bulk_read(yas5xx->map, YAS5XX_MEASURE_DATA,
  284. data, sizeof(data));
  285. if (ret)
  286. goto out_unlock;
  287. mutex_unlock(&yas5xx->lock);
  288. switch (ci->devid) {
  289. case YAS530_DEVICE_ID:
  290. /*
  291. * The t value is 9 bits in big endian format
  292. * These are the bits used in a 16bit word:
  293. * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  294. * x x x x x x x x x
  295. */
  296. val = get_unaligned_be16(&data[0]);
  297. val = FIELD_GET(GENMASK(14, 6), val);
  298. *t = val;
  299. *x = yas530_extract_axis(&data[2]);
  300. *y1 = yas530_extract_axis(&data[4]);
  301. *y2 = yas530_extract_axis(&data[6]);
  302. break;
  303. case YAS532_DEVICE_ID:
  304. /*
  305. * The t value is 10 bits in big endian format
  306. * These are the bits used in a 16bit word:
  307. * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  308. * x x x x x x x x x x
  309. */
  310. val = get_unaligned_be16(&data[0]);
  311. val = FIELD_GET(GENMASK(14, 5), val);
  312. *t = val;
  313. *x = yas532_extract_axis(&data[2]);
  314. *y1 = yas532_extract_axis(&data[4]);
  315. *y2 = yas532_extract_axis(&data[6]);
  316. break;
  317. default:
  318. dev_err(yas5xx->dev, "unknown data format\n");
  319. ret = -EINVAL;
  320. break;
  321. }
  322. return ret;
  323. out_unlock:
  324. mutex_unlock(&yas5xx->lock);
  325. return ret;
  326. }
  327. /**
  328. * yas537_measure() - Make a measure from the hardware
  329. * @yas5xx: The device state
  330. * @t: the raw temperature measurement
  331. * @x: the raw x axis measurement
  332. * @y1: the y1 axis measurement
  333. * @y2: the y2 axis measurement
  334. * @return: 0 on success or error code
  335. */
  336. static int yas537_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y2)
  337. {
  338. struct yas5xx_calibration *c = &yas5xx->calibration;
  339. unsigned int busy;
  340. u8 data[8];
  341. u16 xy1y2[3];
  342. s32 h[3], s[3];
  343. int i, ret;
  344. mutex_lock(&yas5xx->lock);
  345. /* Contrary to YAS530/532, also a "cont" bit is set, meaning unknown */
  346. ret = regmap_write(yas5xx->map, YAS537_MEASURE, YAS5XX_MEASURE_START |
  347. YAS5XX_MEASURE_CONT);
  348. if (ret < 0)
  349. goto out_unlock;
  350. /* Use same timeout like YAS530/532 but the bit is in data row 2 */
  351. ret = regmap_read_poll_timeout(yas5xx->map, YAS5XX_MEASURE_DATA + 2, busy,
  352. !(busy & YAS5XX_MEASURE_DATA_BUSY),
  353. 500, 20000);
  354. if (ret) {
  355. dev_err(yas5xx->dev, "timeout waiting for measurement\n");
  356. goto out_unlock;
  357. }
  358. ret = regmap_bulk_read(yas5xx->map, YAS5XX_MEASURE_DATA,
  359. data, sizeof(data));
  360. if (ret)
  361. goto out_unlock;
  362. mutex_unlock(&yas5xx->lock);
  363. *t = get_unaligned_be16(&data[0]);
  364. xy1y2[0] = FIELD_GET(GENMASK(13, 0), get_unaligned_be16(&data[2]));
  365. xy1y2[1] = get_unaligned_be16(&data[4]);
  366. xy1y2[2] = get_unaligned_be16(&data[6]);
  367. /* The second version of YAS537 needs to include calibration coefficients */
  368. if (yas5xx->version == YAS537_VERSION_1) {
  369. for (i = 0; i < 3; i++)
  370. s[i] = xy1y2[i] - BIT(13);
  371. h[0] = (c->k * (128 * s[0] + c->a2 * s[1] + c->a3 * s[2])) / BIT(13);
  372. h[1] = (c->k * (c->a4 * s[0] + c->a5 * s[1] + c->a6 * s[2])) / BIT(13);
  373. h[2] = (c->k * (c->a7 * s[0] + c->a8 * s[1] + c->a9 * s[2])) / BIT(13);
  374. for (i = 0; i < 3; i++) {
  375. clamp_val(h[i], -BIT(13), BIT(13) - 1);
  376. xy1y2[i] = h[i] + BIT(13);
  377. }
  378. }
  379. *x = xy1y2[0];
  380. *y1 = xy1y2[1];
  381. *y2 = xy1y2[2];
  382. return 0;
  383. out_unlock:
  384. mutex_unlock(&yas5xx->lock);
  385. return ret;
  386. }
  387. /* Used by YAS530, YAS532 and YAS533 */
  388. static s32 yas530_linearize(struct yas5xx *yas5xx, u16 val, int axis)
  389. {
  390. const struct yas5xx_chip_info *ci = yas5xx->chip_info;
  391. struct yas5xx_calibration *c = &yas5xx->calibration;
  392. static const s32 yas532ac_coef[] = {
  393. YAS532_VERSION_AC_COEF_X,
  394. YAS532_VERSION_AC_COEF_Y1,
  395. YAS532_VERSION_AC_COEF_Y2,
  396. };
  397. s32 coef;
  398. /* Select coefficients */
  399. switch (ci->devid) {
  400. case YAS530_DEVICE_ID:
  401. if (yas5xx->version == YAS530_VERSION_A)
  402. coef = YAS530_VERSION_A_COEF;
  403. else
  404. coef = YAS530_VERSION_B_COEF;
  405. break;
  406. case YAS532_DEVICE_ID:
  407. if (yas5xx->version == YAS532_VERSION_AB)
  408. coef = YAS532_VERSION_AB_COEF;
  409. else
  410. /* Elaborate coefficients */
  411. coef = yas532ac_coef[axis];
  412. break;
  413. default:
  414. dev_err(yas5xx->dev, "unknown device type\n");
  415. return val;
  416. }
  417. /*
  418. * Linearization formula:
  419. *
  420. * x' = x - (3721 + 50 * f) + (xoffset - r) * c
  421. *
  422. * Where f and r are calibration values, c is a per-device
  423. * and sometimes per-axis coefficient.
  424. */
  425. return val - (3721 + 50 * c->f[axis]) +
  426. (yas5xx->hard_offsets[axis] - c->r[axis]) * coef;
  427. }
  428. static s32 yas5xx_calc_temperature(struct yas5xx *yas5xx, u16 t)
  429. {
  430. const struct yas5xx_chip_info *ci = yas5xx->chip_info;
  431. s32 to;
  432. u16 t_ref;
  433. s16 min_temp_x10;
  434. int ref_temp_x10;
  435. t_ref = ci->t_ref;
  436. min_temp_x10 = ci->min_temp_x10;
  437. ref_temp_x10 = 200;
  438. to = (min_temp_x10 + ((ref_temp_x10 - min_temp_x10) * t / t_ref)) * 100;
  439. return to;
  440. }
  441. /**
  442. * yas530_get_measure() - Measure a sample of all axis and process
  443. * @yas5xx: The device state
  444. * @to: Temperature out
  445. * @xo: X axis out
  446. * @yo: Y axis out
  447. * @zo: Z axis out
  448. * @return: 0 on success or error code
  449. *
  450. * Used by YAS530, YAS532 and YAS533.
  451. */
  452. static int yas530_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo)
  453. {
  454. const struct yas5xx_chip_info *ci = yas5xx->chip_info;
  455. struct yas5xx_calibration *c = &yas5xx->calibration;
  456. u16 t_ref, t_comp, t, x, y1, y2;
  457. /* These are signed x, signed y1 etc */
  458. s32 sx, sy1, sy2, sy, sz;
  459. int ret;
  460. /* We first get raw data that needs to be translated to [x,y,z] */
  461. ret = yas530_measure(yas5xx, &t, &x, &y1, &y2);
  462. if (ret)
  463. return ret;
  464. /* Do some linearization if available */
  465. sx = yas530_linearize(yas5xx, x, 0);
  466. sy1 = yas530_linearize(yas5xx, y1, 1);
  467. sy2 = yas530_linearize(yas5xx, y2, 2);
  468. /*
  469. * Set the temperature for compensation (unit: counts):
  470. * YAS532/YAS533 version AC uses the temperature deviation as a
  471. * multiplier. YAS530 and YAS532 version AB use solely the t value.
  472. */
  473. t_ref = ci->t_ref;
  474. if (ci->devid == YAS532_DEVICE_ID &&
  475. yas5xx->version == YAS532_VERSION_AC) {
  476. t_comp = t - t_ref;
  477. } else {
  478. t_comp = t;
  479. }
  480. /*
  481. * Temperature compensation for x, y1, y2 respectively:
  482. *
  483. * Cx * t_comp
  484. * x' = x - -----------
  485. * 100
  486. */
  487. sx = sx - (c->Cx * t_comp) / 100;
  488. sy1 = sy1 - (c->Cy1 * t_comp) / 100;
  489. sy2 = sy2 - (c->Cy2 * t_comp) / 100;
  490. /*
  491. * Break y1 and y2 into y and z, y1 and y2 are apparently encoding
  492. * y and z.
  493. */
  494. sy = sy1 - sy2;
  495. sz = -sy1 - sy2;
  496. /* Calculate temperature readout */
  497. *to = yas5xx_calc_temperature(yas5xx, t);
  498. /*
  499. * Calibrate [x,y,z] with some formulas like this:
  500. *
  501. * 100 * x + a_2 * y + a_3 * z
  502. * x' = k * ---------------------------
  503. * 10
  504. *
  505. * a_4 * x + a_5 * y + a_6 * z
  506. * y' = k * ---------------------------
  507. * 10
  508. *
  509. * a_7 * x + a_8 * y + a_9 * z
  510. * z' = k * ---------------------------
  511. * 10
  512. */
  513. *xo = c->k * ((100 * sx + c->a2 * sy + c->a3 * sz) / 10);
  514. *yo = c->k * ((c->a4 * sx + c->a5 * sy + c->a6 * sz) / 10);
  515. *zo = c->k * ((c->a7 * sx + c->a8 * sy + c->a9 * sz) / 10);
  516. return 0;
  517. }
  518. /**
  519. * yas537_get_measure() - Measure a sample of all axis and process
  520. * @yas5xx: The device state
  521. * @to: Temperature out
  522. * @xo: X axis out
  523. * @yo: Y axis out
  524. * @zo: Z axis out
  525. * @return: 0 on success or error code
  526. */
  527. static int yas537_get_measure(struct yas5xx *yas5xx, s32 *to, s32 *xo, s32 *yo, s32 *zo)
  528. {
  529. u16 t, x, y1, y2;
  530. int ret;
  531. /* We first get raw data that needs to be translated to [x,y,z] */
  532. ret = yas537_measure(yas5xx, &t, &x, &y1, &y2);
  533. if (ret)
  534. return ret;
  535. /* Calculate temperature readout */
  536. *to = yas5xx_calc_temperature(yas5xx, t);
  537. /*
  538. * Unfortunately, no linearization or temperature compensation formulas
  539. * are known for YAS537.
  540. */
  541. /* Calculate x, y, z from x, y1, y2 */
  542. *xo = (x - BIT(13)) * 300;
  543. *yo = (y1 - y2) * 1732 / 10;
  544. *zo = (-y1 - y2 + BIT(14)) * 300;
  545. return 0;
  546. }
  547. static int yas5xx_read_raw(struct iio_dev *indio_dev,
  548. struct iio_chan_spec const *chan,
  549. int *val, int *val2,
  550. long mask)
  551. {
  552. struct yas5xx *yas5xx = iio_priv(indio_dev);
  553. const struct yas5xx_chip_info *ci = yas5xx->chip_info;
  554. s32 t, x, y, z;
  555. int ret;
  556. switch (mask) {
  557. case IIO_CHAN_INFO_PROCESSED:
  558. case IIO_CHAN_INFO_RAW:
  559. pm_runtime_get_sync(yas5xx->dev);
  560. ret = ci->get_measure(yas5xx, &t, &x, &y, &z);
  561. pm_runtime_mark_last_busy(yas5xx->dev);
  562. pm_runtime_put_autosuspend(yas5xx->dev);
  563. if (ret)
  564. return ret;
  565. switch (chan->address) {
  566. case 0:
  567. *val = t;
  568. break;
  569. case 1:
  570. *val = x;
  571. break;
  572. case 2:
  573. *val = y;
  574. break;
  575. case 3:
  576. *val = z;
  577. break;
  578. default:
  579. dev_err(yas5xx->dev, "unknown channel\n");
  580. return -EINVAL;
  581. }
  582. return IIO_VAL_INT;
  583. case IIO_CHAN_INFO_SCALE:
  584. *val = 1;
  585. *val2 = ci->scaling_val2;
  586. return IIO_VAL_FRACTIONAL;
  587. default:
  588. /* Unknown request */
  589. return -EINVAL;
  590. }
  591. }
  592. static void yas5xx_fill_buffer(struct iio_dev *indio_dev)
  593. {
  594. struct yas5xx *yas5xx = iio_priv(indio_dev);
  595. const struct yas5xx_chip_info *ci = yas5xx->chip_info;
  596. s32 t, x, y, z;
  597. int ret;
  598. pm_runtime_get_sync(yas5xx->dev);
  599. ret = ci->get_measure(yas5xx, &t, &x, &y, &z);
  600. pm_runtime_mark_last_busy(yas5xx->dev);
  601. pm_runtime_put_autosuspend(yas5xx->dev);
  602. if (ret) {
  603. dev_err(yas5xx->dev, "error refilling buffer\n");
  604. return;
  605. }
  606. yas5xx->scan.channels[0] = t;
  607. yas5xx->scan.channels[1] = x;
  608. yas5xx->scan.channels[2] = y;
  609. yas5xx->scan.channels[3] = z;
  610. iio_push_to_buffers_with_timestamp(indio_dev, &yas5xx->scan,
  611. iio_get_time_ns(indio_dev));
  612. }
  613. static irqreturn_t yas5xx_handle_trigger(int irq, void *p)
  614. {
  615. const struct iio_poll_func *pf = p;
  616. struct iio_dev *indio_dev = pf->indio_dev;
  617. yas5xx_fill_buffer(indio_dev);
  618. iio_trigger_notify_done(indio_dev->trig);
  619. return IRQ_HANDLED;
  620. }
  621. static const struct iio_mount_matrix *
  622. yas5xx_get_mount_matrix(const struct iio_dev *indio_dev,
  623. const struct iio_chan_spec *chan)
  624. {
  625. struct yas5xx *yas5xx = iio_priv(indio_dev);
  626. return &yas5xx->orientation;
  627. }
  628. static const struct iio_chan_spec_ext_info yas5xx_ext_info[] = {
  629. IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, yas5xx_get_mount_matrix),
  630. { }
  631. };
  632. #define YAS5XX_AXIS_CHANNEL(axis, index) \
  633. { \
  634. .type = IIO_MAGN, \
  635. .modified = 1, \
  636. .channel2 = IIO_MOD_##axis, \
  637. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  638. BIT(IIO_CHAN_INFO_SCALE), \
  639. .ext_info = yas5xx_ext_info, \
  640. .address = index, \
  641. .scan_index = index, \
  642. .scan_type = { \
  643. .sign = 's', \
  644. .realbits = 32, \
  645. .storagebits = 32, \
  646. .endianness = IIO_CPU, \
  647. }, \
  648. }
  649. static const struct iio_chan_spec yas5xx_channels[] = {
  650. {
  651. .type = IIO_TEMP,
  652. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  653. .address = 0,
  654. .scan_index = 0,
  655. .scan_type = {
  656. .sign = 's',
  657. .realbits = 32,
  658. .storagebits = 32,
  659. .endianness = IIO_CPU,
  660. },
  661. },
  662. YAS5XX_AXIS_CHANNEL(X, 1),
  663. YAS5XX_AXIS_CHANNEL(Y, 2),
  664. YAS5XX_AXIS_CHANNEL(Z, 3),
  665. IIO_CHAN_SOFT_TIMESTAMP(4),
  666. };
  667. static const unsigned long yas5xx_scan_masks[] = { GENMASK(3, 0), 0 };
  668. static const struct iio_info yas5xx_info = {
  669. .read_raw = &yas5xx_read_raw,
  670. };
  671. static bool yas5xx_volatile_reg(struct device *dev, unsigned int reg)
  672. {
  673. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  674. struct yas5xx *yas5xx = iio_priv(indio_dev);
  675. const struct yas5xx_chip_info *ci = yas5xx->chip_info;
  676. int reg_qty;
  677. int i;
  678. if (reg >= YAS5XX_MEASURE_DATA && reg < YAS5XX_MEASURE_DATA + 8)
  679. return true;
  680. /*
  681. * YAS versions share different registers on the same address,
  682. * need to differentiate.
  683. */
  684. reg_qty = ci->volatile_reg_qty;
  685. for (i = 0; i < reg_qty; i++) {
  686. if (reg == ci->volatile_reg[i])
  687. return true;
  688. }
  689. return false;
  690. }
  691. /* TODO: enable regmap cache, using mark dirty and sync at runtime resume */
  692. static const struct regmap_config yas5xx_regmap_config = {
  693. .reg_bits = 8,
  694. .val_bits = 8,
  695. .max_register = 0xff,
  696. .volatile_reg = yas5xx_volatile_reg,
  697. };
  698. /**
  699. * yas530_extract_calibration() - extracts the a2-a9 and k calibration
  700. * @data: the bitfield to use
  701. * @c: the calibration to populate
  702. *
  703. * Used by YAS530, YAS532 and YAS533.
  704. */
  705. static void yas530_extract_calibration(u8 *data, struct yas5xx_calibration *c)
  706. {
  707. u64 val = get_unaligned_be64(data);
  708. /*
  709. * Bitfield layout for the axis calibration data, for factor
  710. * a2 = 2 etc, k = k, c = clock divider
  711. *
  712. * n 7 6 5 4 3 2 1 0
  713. * 0 [ 2 2 2 2 2 2 3 3 ] bits 63 .. 56
  714. * 1 [ 3 3 4 4 4 4 4 4 ] bits 55 .. 48
  715. * 2 [ 5 5 5 5 5 5 6 6 ] bits 47 .. 40
  716. * 3 [ 6 6 6 6 7 7 7 7 ] bits 39 .. 32
  717. * 4 [ 7 7 7 8 8 8 8 8 ] bits 31 .. 24
  718. * 5 [ 8 9 9 9 9 9 9 9 ] bits 23 .. 16
  719. * 6 [ 9 k k k k k c c ] bits 15 .. 8
  720. * 7 [ c x x x x x x x ] bits 7 .. 0
  721. */
  722. c->a2 = FIELD_GET(GENMASK_ULL(63, 58), val) - 32;
  723. c->a3 = FIELD_GET(GENMASK_ULL(57, 54), val) - 8;
  724. c->a4 = FIELD_GET(GENMASK_ULL(53, 48), val) - 32;
  725. c->a5 = FIELD_GET(GENMASK_ULL(47, 42), val) + 38;
  726. c->a6 = FIELD_GET(GENMASK_ULL(41, 36), val) - 32;
  727. c->a7 = FIELD_GET(GENMASK_ULL(35, 29), val) - 64;
  728. c->a8 = FIELD_GET(GENMASK_ULL(28, 23), val) - 32;
  729. c->a9 = FIELD_GET(GENMASK_ULL(22, 15), val);
  730. c->k = FIELD_GET(GENMASK_ULL(14, 10), val) + 10;
  731. c->dck = FIELD_GET(GENMASK_ULL(9, 7), val);
  732. }
  733. static int yas530_get_calibration_data(struct yas5xx *yas5xx)
  734. {
  735. struct yas5xx_calibration *c = &yas5xx->calibration;
  736. u8 data[16];
  737. u32 val;
  738. int ret;
  739. /* Dummy read, first read is ALWAYS wrong */
  740. ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data));
  741. if (ret)
  742. return ret;
  743. /* Actual calibration readout */
  744. ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data));
  745. if (ret)
  746. return ret;
  747. dev_dbg(yas5xx->dev, "calibration data: %16ph\n", data);
  748. /* Contribute calibration data to the input pool for kernel entropy */
  749. add_device_randomness(data, sizeof(data));
  750. /* Extract version */
  751. yas5xx->version = data[15] & GENMASK(1, 0);
  752. /* Extract the calibration from the bitfield */
  753. c->Cx = data[0] * 6 - 768;
  754. c->Cy1 = data[1] * 6 - 768;
  755. c->Cy2 = data[2] * 6 - 768;
  756. yas530_extract_calibration(&data[3], c);
  757. /*
  758. * Extract linearization:
  759. * Linearization layout in the 32 bits at byte 11:
  760. * The r factors are 6 bit values where bit 5 is the sign
  761. *
  762. * n 7 6 5 4 3 2 1 0
  763. * 0 [ xx xx xx r0 r0 r0 r0 r0 ] bits 31 .. 24
  764. * 1 [ r0 f0 f0 r1 r1 r1 r1 r1 ] bits 23 .. 16
  765. * 2 [ r1 f1 f1 r2 r2 r2 r2 r2 ] bits 15 .. 8
  766. * 3 [ r2 f2 f2 xx xx xx xx xx ] bits 7 .. 0
  767. */
  768. val = get_unaligned_be32(&data[11]);
  769. c->f[0] = FIELD_GET(GENMASK(22, 21), val);
  770. c->f[1] = FIELD_GET(GENMASK(14, 13), val);
  771. c->f[2] = FIELD_GET(GENMASK(6, 5), val);
  772. c->r[0] = sign_extend32(FIELD_GET(GENMASK(28, 23), val), 5);
  773. c->r[1] = sign_extend32(FIELD_GET(GENMASK(20, 15), val), 5);
  774. c->r[2] = sign_extend32(FIELD_GET(GENMASK(12, 7), val), 5);
  775. return 0;
  776. }
  777. static int yas532_get_calibration_data(struct yas5xx *yas5xx)
  778. {
  779. struct yas5xx_calibration *c = &yas5xx->calibration;
  780. u8 data[14];
  781. u32 val;
  782. int ret;
  783. /* Dummy read, first read is ALWAYS wrong */
  784. ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data));
  785. if (ret)
  786. return ret;
  787. /* Actual calibration readout */
  788. ret = regmap_bulk_read(yas5xx->map, YAS530_CAL, data, sizeof(data));
  789. if (ret)
  790. return ret;
  791. dev_dbg(yas5xx->dev, "calibration data: %14ph\n", data);
  792. /* Sanity check, is this all zeroes? */
  793. if (!memchr_inv(data, 0x00, 13) && !(data[13] & BIT(7)))
  794. dev_warn(yas5xx->dev, "calibration is blank!\n");
  795. /* Contribute calibration data to the input pool for kernel entropy */
  796. add_device_randomness(data, sizeof(data));
  797. /* Only one bit of version info reserved here as far as we know */
  798. yas5xx->version = data[13] & BIT(0);
  799. /* Extract calibration from the bitfield */
  800. c->Cx = data[0] * 10 - 1280;
  801. c->Cy1 = data[1] * 10 - 1280;
  802. c->Cy2 = data[2] * 10 - 1280;
  803. yas530_extract_calibration(&data[3], c);
  804. /*
  805. * Extract linearization:
  806. * Linearization layout in the 32 bits at byte 10:
  807. * The r factors are 6 bit values where bit 5 is the sign
  808. *
  809. * n 7 6 5 4 3 2 1 0
  810. * 0 [ xx r0 r0 r0 r0 r0 r0 f0 ] bits 31 .. 24
  811. * 1 [ f0 r1 r1 r1 r1 r1 r1 f1 ] bits 23 .. 16
  812. * 2 [ f1 r2 r2 r2 r2 r2 r2 f2 ] bits 15 .. 8
  813. * 3 [ f2 xx xx xx xx xx xx xx ] bits 7 .. 0
  814. */
  815. val = get_unaligned_be32(&data[10]);
  816. c->f[0] = FIELD_GET(GENMASK(24, 23), val);
  817. c->f[1] = FIELD_GET(GENMASK(16, 15), val);
  818. c->f[2] = FIELD_GET(GENMASK(8, 7), val);
  819. c->r[0] = sign_extend32(FIELD_GET(GENMASK(30, 25), val), 5);
  820. c->r[1] = sign_extend32(FIELD_GET(GENMASK(22, 17), val), 5);
  821. c->r[2] = sign_extend32(FIELD_GET(GENMASK(14, 7), val), 5);
  822. return 0;
  823. }
  824. static int yas537_get_calibration_data(struct yas5xx *yas5xx)
  825. {
  826. struct yas5xx_calibration *c = &yas5xx->calibration;
  827. u8 data[17];
  828. u32 val1, val2, val3, val4;
  829. int i, ret;
  830. /* Writing SRST register */
  831. ret = regmap_write(yas5xx->map, YAS537_SRST, BIT(1));
  832. if (ret)
  833. return ret;
  834. /* Calibration readout, YAS537 needs one readout only */
  835. ret = regmap_bulk_read(yas5xx->map, YAS537_CAL, data, sizeof(data));
  836. if (ret)
  837. return ret;
  838. dev_dbg(yas5xx->dev, "calibration data: %17ph\n", data);
  839. /* Sanity check, is this all zeroes? */
  840. if (!memchr_inv(data, 0x00, 16) && !FIELD_GET(GENMASK(5, 0), data[16]))
  841. dev_warn(yas5xx->dev, "calibration is blank!\n");
  842. /* Contribute calibration data to the input pool for kernel entropy */
  843. add_device_randomness(data, sizeof(data));
  844. /* Extract version information */
  845. yas5xx->version = FIELD_GET(GENMASK(7, 6), data[16]);
  846. /* There are two versions of YAS537 behaving differently */
  847. switch (yas5xx->version) {
  848. case YAS537_VERSION_0:
  849. /*
  850. * The first version simply writes data back into registers:
  851. *
  852. * data[0] YAS537_MTC 0x93
  853. * data[1] 0x94
  854. * data[2] 0x95
  855. * data[3] 0x96
  856. * data[4] 0x97
  857. * data[5] 0x98
  858. * data[6] 0x99
  859. * data[7] 0x9a
  860. * data[8] 0x9b
  861. * data[9] 0x9c
  862. * data[10] 0x9d
  863. * data[11] YAS537_OC 0x9e
  864. *
  865. * data[12] YAS537_OFFSET_X 0x84
  866. * data[13] YAS537_OFFSET_Y1 0x85
  867. * data[14] YAS537_OFFSET_Y2 0x86
  868. *
  869. * data[15] YAS537_HCK 0x88
  870. * data[16] YAS537_LCK 0x89
  871. */
  872. for (i = 0; i < 12; i++) {
  873. ret = regmap_write(yas5xx->map, YAS537_MTC + i,
  874. data[i]);
  875. if (ret)
  876. return ret;
  877. }
  878. for (i = 0; i < 3; i++) {
  879. ret = regmap_write(yas5xx->map, YAS537_OFFSET_X + i,
  880. data[i + 12]);
  881. if (ret)
  882. return ret;
  883. yas5xx->hard_offsets[i] = data[i + 12];
  884. }
  885. for (i = 0; i < 2; i++) {
  886. ret = regmap_write(yas5xx->map, YAS537_HCK + i,
  887. data[i + 15]);
  888. if (ret)
  889. return ret;
  890. }
  891. break;
  892. case YAS537_VERSION_1:
  893. /*
  894. * The second version writes some data into registers but also
  895. * extracts calibration coefficients.
  896. *
  897. * Registers being written:
  898. *
  899. * data[0] YAS537_MTC 0x93
  900. * data[1] YAS537_MTC+1 0x94
  901. * data[2] YAS537_MTC+2 0x95
  902. * data[3] YAS537_MTC+3 (partially) 0x96
  903. *
  904. * data[12] YAS537_OFFSET_X 0x84
  905. * data[13] YAS537_OFFSET_Y1 0x85
  906. * data[14] YAS537_OFFSET_Y2 0x86
  907. *
  908. * data[15] YAS537_HCK (partially) 0x88
  909. * YAS537_LCK (partially) 0x89
  910. * data[16] YAS537_OC (partially) 0x9e
  911. */
  912. for (i = 0; i < 3; i++) {
  913. ret = regmap_write(yas5xx->map, YAS537_MTC + i,
  914. data[i]);
  915. if (ret)
  916. return ret;
  917. }
  918. for (i = 0; i < 3; i++) {
  919. ret = regmap_write(yas5xx->map, YAS537_OFFSET_X + i,
  920. data[i + 12]);
  921. if (ret)
  922. return ret;
  923. yas5xx->hard_offsets[i] = data[i + 12];
  924. }
  925. /*
  926. * Visualization of partially taken data:
  927. *
  928. * data[3] n 7 6 5 4 3 2 1 0
  929. * YAS537_MTC+3 x x x 1 0 0 0 0
  930. *
  931. * data[15] n 7 6 5 4 3 2 1 0
  932. * YAS537_HCK x x x x 0
  933. *
  934. * data[15] n 7 6 5 4 3 2 1 0
  935. * YAS537_LCK x x x x 0
  936. *
  937. * data[16] n 7 6 5 4 3 2 1 0
  938. * YAS537_OC x x x x x x
  939. */
  940. ret = regmap_write(yas5xx->map, YAS537_MTC + 3,
  941. FIELD_PREP(YAS537_MTC3_MASK_PREP,
  942. FIELD_GET(YAS537_MTC3_MASK_GET, data[3])) |
  943. YAS537_MTC3_ADD_BIT);
  944. if (ret)
  945. return ret;
  946. ret = regmap_write(yas5xx->map, YAS537_HCK,
  947. FIELD_PREP(YAS537_HCK_MASK_PREP,
  948. FIELD_GET(YAS537_HCK_MASK_GET, data[15])));
  949. if (ret)
  950. return ret;
  951. ret = regmap_write(yas5xx->map, YAS537_LCK,
  952. FIELD_PREP(YAS537_LCK_MASK_PREP,
  953. FIELD_GET(YAS537_LCK_MASK_GET, data[15])));
  954. if (ret)
  955. return ret;
  956. ret = regmap_write(yas5xx->map, YAS537_OC,
  957. FIELD_GET(YAS537_OC_MASK_GET, data[16]));
  958. if (ret)
  959. return ret;
  960. /*
  961. * For data extraction, build some blocks. Four 32-bit blocks
  962. * look appropriate.
  963. *
  964. * n 7 6 5 4 3 2 1 0
  965. * data[0] 0 [ Cx Cx Cx Cx Cx Cx Cx Cx ] bits 31 .. 24
  966. * data[1] 1 [ Cx C1 C1 C1 C1 C1 C1 C1 ] bits 23 .. 16
  967. * data[2] 2 [ C1 C1 C2 C2 C2 C2 C2 C2 ] bits 15 .. 8
  968. * data[3] 3 [ C2 C2 C2 ] bits 7 .. 0
  969. *
  970. * n 7 6 5 4 3 2 1 0
  971. * data[3] 0 [ a2 a2 a2 a2 a2 ] bits 31 .. 24
  972. * data[4] 1 [ a2 a2 a3 a3 a3 a3 a3 a3 ] bits 23 .. 16
  973. * data[5] 2 [ a3 a4 a4 a4 a4 a4 a4 a4 ] bits 15 .. 8
  974. * data[6] 3 [ a4 ] bits 7 .. 0
  975. *
  976. * n 7 6 5 4 3 2 1 0
  977. * data[6] 0 [ a5 a5 a5 a5 a5 a5 a5 ] bits 31 .. 24
  978. * data[7] 1 [ a5 a5 a6 a6 a6 a6 a6 a6 ] bits 23 .. 16
  979. * data[8] 2 [ a6 a7 a7 a7 a7 a7 a7 a7 ] bits 15 .. 8
  980. * data[9] 3 [ a7 ] bits 7 .. 0
  981. *
  982. * n 7 6 5 4 3 2 1 0
  983. * data[9] 0 [ a8 a8 a8 a8 a8 a8 a8 ] bits 31 .. 24
  984. * data[10] 1 [ a9 a9 a9 a9 a9 a9 a9 a9 ] bits 23 .. 16
  985. * data[11] 2 [ a9 k k k k k k k ] bits 15 .. 8
  986. * data[12] 3 [ ] bits 7 .. 0
  987. */
  988. val1 = get_unaligned_be32(&data[0]);
  989. val2 = get_unaligned_be32(&data[3]);
  990. val3 = get_unaligned_be32(&data[6]);
  991. val4 = get_unaligned_be32(&data[9]);
  992. /* Extract calibration coefficients and modify */
  993. c->Cx = FIELD_GET(GENMASK(31, 23), val1) - 256;
  994. c->Cy1 = FIELD_GET(GENMASK(22, 14), val1) - 256;
  995. c->Cy2 = FIELD_GET(GENMASK(13, 5), val1) - 256;
  996. c->a2 = FIELD_GET(GENMASK(28, 22), val2) - 64;
  997. c->a3 = FIELD_GET(GENMASK(21, 15), val2) - 64;
  998. c->a4 = FIELD_GET(GENMASK(14, 7), val2) - 128;
  999. c->a5 = FIELD_GET(GENMASK(30, 22), val3) - 112;
  1000. c->a6 = FIELD_GET(GENMASK(21, 15), val3) - 64;
  1001. c->a7 = FIELD_GET(GENMASK(14, 7), val3) - 128;
  1002. c->a8 = FIELD_GET(GENMASK(30, 24), val4) - 64;
  1003. c->a9 = FIELD_GET(GENMASK(23, 15), val4) - 112;
  1004. c->k = FIELD_GET(GENMASK(14, 8), val4);
  1005. break;
  1006. default:
  1007. dev_err(yas5xx->dev, "unknown version of YAS537\n");
  1008. return -EINVAL;
  1009. }
  1010. return 0;
  1011. }
  1012. /* Used by YAS530, YAS532 and YAS533 */
  1013. static void yas530_dump_calibration(struct yas5xx *yas5xx)
  1014. {
  1015. struct yas5xx_calibration *c = &yas5xx->calibration;
  1016. dev_dbg(yas5xx->dev, "f[] = [%d, %d, %d]\n",
  1017. c->f[0], c->f[1], c->f[2]);
  1018. dev_dbg(yas5xx->dev, "r[] = [%d, %d, %d]\n",
  1019. c->r[0], c->r[1], c->r[2]);
  1020. dev_dbg(yas5xx->dev, "Cx = %d\n", c->Cx);
  1021. dev_dbg(yas5xx->dev, "Cy1 = %d\n", c->Cy1);
  1022. dev_dbg(yas5xx->dev, "Cy2 = %d\n", c->Cy2);
  1023. dev_dbg(yas5xx->dev, "a2 = %d\n", c->a2);
  1024. dev_dbg(yas5xx->dev, "a3 = %d\n", c->a3);
  1025. dev_dbg(yas5xx->dev, "a4 = %d\n", c->a4);
  1026. dev_dbg(yas5xx->dev, "a5 = %d\n", c->a5);
  1027. dev_dbg(yas5xx->dev, "a6 = %d\n", c->a6);
  1028. dev_dbg(yas5xx->dev, "a7 = %d\n", c->a7);
  1029. dev_dbg(yas5xx->dev, "a8 = %d\n", c->a8);
  1030. dev_dbg(yas5xx->dev, "a9 = %d\n", c->a9);
  1031. dev_dbg(yas5xx->dev, "k = %d\n", c->k);
  1032. dev_dbg(yas5xx->dev, "dck = %d\n", c->dck);
  1033. }
  1034. static void yas537_dump_calibration(struct yas5xx *yas5xx)
  1035. {
  1036. struct yas5xx_calibration *c = &yas5xx->calibration;
  1037. if (yas5xx->version == YAS537_VERSION_1) {
  1038. dev_dbg(yas5xx->dev, "Cx = %d\n", c->Cx);
  1039. dev_dbg(yas5xx->dev, "Cy1 = %d\n", c->Cy1);
  1040. dev_dbg(yas5xx->dev, "Cy2 = %d\n", c->Cy2);
  1041. dev_dbg(yas5xx->dev, "a2 = %d\n", c->a2);
  1042. dev_dbg(yas5xx->dev, "a3 = %d\n", c->a3);
  1043. dev_dbg(yas5xx->dev, "a4 = %d\n", c->a4);
  1044. dev_dbg(yas5xx->dev, "a5 = %d\n", c->a5);
  1045. dev_dbg(yas5xx->dev, "a6 = %d\n", c->a6);
  1046. dev_dbg(yas5xx->dev, "a7 = %d\n", c->a7);
  1047. dev_dbg(yas5xx->dev, "a8 = %d\n", c->a8);
  1048. dev_dbg(yas5xx->dev, "a9 = %d\n", c->a9);
  1049. dev_dbg(yas5xx->dev, "k = %d\n", c->k);
  1050. }
  1051. }
  1052. /* Used by YAS530, YAS532 and YAS533 */
  1053. static int yas530_set_offsets(struct yas5xx *yas5xx, s8 ox, s8 oy1, s8 oy2)
  1054. {
  1055. int ret;
  1056. ret = regmap_write(yas5xx->map, YAS530_OFFSET_X, ox);
  1057. if (ret)
  1058. return ret;
  1059. ret = regmap_write(yas5xx->map, YAS530_OFFSET_Y1, oy1);
  1060. if (ret)
  1061. return ret;
  1062. return regmap_write(yas5xx->map, YAS530_OFFSET_Y2, oy2);
  1063. }
  1064. /* Used by YAS530, YAS532 and YAS533 */
  1065. static s8 yas530_adjust_offset(s8 old, int bit, u16 center, u16 measure)
  1066. {
  1067. if (measure > center)
  1068. return old + BIT(bit);
  1069. if (measure < center)
  1070. return old - BIT(bit);
  1071. return old;
  1072. }
  1073. /* Used by YAS530, YAS532 and YAS533 */
  1074. static int yas530_measure_offsets(struct yas5xx *yas5xx)
  1075. {
  1076. const struct yas5xx_chip_info *ci = yas5xx->chip_info;
  1077. int ret;
  1078. u16 center;
  1079. u16 t, x, y1, y2;
  1080. s8 ox, oy1, oy2;
  1081. int i;
  1082. /* Actuate the init coil and measure offsets */
  1083. ret = regmap_write(yas5xx->map, YAS530_ACTUATE_INIT_COIL, 0);
  1084. if (ret)
  1085. return ret;
  1086. /* When the initcoil is active this should be around the center */
  1087. switch (ci->devid) {
  1088. case YAS530_DEVICE_ID:
  1089. center = YAS530_DATA_CENTER;
  1090. break;
  1091. case YAS532_DEVICE_ID:
  1092. center = YAS532_DATA_CENTER;
  1093. break;
  1094. default:
  1095. dev_err(yas5xx->dev, "unknown device type\n");
  1096. return -EINVAL;
  1097. }
  1098. /*
  1099. * We set offsets in the interval +-31 by iterating
  1100. * +-16, +-8, +-4, +-2, +-1 adjusting the offsets each
  1101. * time, then writing the final offsets into the
  1102. * registers.
  1103. *
  1104. * NOTE: these offsets are NOT in the same unit or magnitude
  1105. * as the values for [x, y1, y2]. The value is +/-31
  1106. * but the effect on the raw values is much larger.
  1107. * The effect of the offset is to bring the measure
  1108. * rougly to the center.
  1109. */
  1110. ox = 0;
  1111. oy1 = 0;
  1112. oy2 = 0;
  1113. for (i = 4; i >= 0; i--) {
  1114. ret = yas530_set_offsets(yas5xx, ox, oy1, oy2);
  1115. if (ret)
  1116. return ret;
  1117. ret = yas530_measure(yas5xx, &t, &x, &y1, &y2);
  1118. if (ret)
  1119. return ret;
  1120. dev_dbg(yas5xx->dev, "measurement %d: x=%d, y1=%d, y2=%d\n",
  1121. 5-i, x, y1, y2);
  1122. ox = yas530_adjust_offset(ox, i, center, x);
  1123. oy1 = yas530_adjust_offset(oy1, i, center, y1);
  1124. oy2 = yas530_adjust_offset(oy2, i, center, y2);
  1125. }
  1126. /* Needed for calibration algorithm */
  1127. yas5xx->hard_offsets[0] = ox;
  1128. yas5xx->hard_offsets[1] = oy1;
  1129. yas5xx->hard_offsets[2] = oy2;
  1130. ret = yas530_set_offsets(yas5xx, ox, oy1, oy2);
  1131. if (ret)
  1132. return ret;
  1133. dev_info(yas5xx->dev, "discovered hard offsets: x=%d, y1=%d, y2=%d\n",
  1134. ox, oy1, oy2);
  1135. return 0;
  1136. }
  1137. /* Used by YAS530, YAS532 and YAS533 */
  1138. static int yas530_power_on(struct yas5xx *yas5xx)
  1139. {
  1140. unsigned int val;
  1141. int ret;
  1142. /* Zero the test registers */
  1143. ret = regmap_write(yas5xx->map, YAS530_TEST1, 0);
  1144. if (ret)
  1145. return ret;
  1146. ret = regmap_write(yas5xx->map, YAS530_TEST2, 0);
  1147. if (ret)
  1148. return ret;
  1149. /* Set up for no interrupts, calibrated clock divider */
  1150. val = FIELD_PREP(YAS5XX_CONFIG_CCK_MASK, yas5xx->calibration.dck);
  1151. ret = regmap_write(yas5xx->map, YAS530_CONFIG, val);
  1152. if (ret)
  1153. return ret;
  1154. /* Measure interval 0 (back-to-back?) */
  1155. return regmap_write(yas5xx->map, YAS530_MEASURE_INTERVAL, 0);
  1156. }
  1157. static int yas537_power_on(struct yas5xx *yas5xx)
  1158. {
  1159. __be16 buf;
  1160. int ret;
  1161. u8 intrvl;
  1162. /* Writing ADCCAL and TRM registers */
  1163. buf = cpu_to_be16(GENMASK(9, 3));
  1164. ret = regmap_bulk_write(yas5xx->map, YAS537_ADCCAL, &buf, sizeof(buf));
  1165. if (ret)
  1166. return ret;
  1167. ret = regmap_write(yas5xx->map, YAS537_TRM, GENMASK(7, 0));
  1168. if (ret)
  1169. return ret;
  1170. /* The interval value is static in regular operation */
  1171. intrvl = (YAS537_DEFAULT_SENSOR_DELAY_MS * MILLI
  1172. - YAS537_MEASURE_TIME_WORST_US) / 4100;
  1173. ret = regmap_write(yas5xx->map, YAS537_MEASURE_INTERVAL, intrvl);
  1174. if (ret)
  1175. return ret;
  1176. /* The average value is also static in regular operation */
  1177. ret = regmap_write(yas5xx->map, YAS537_AVR, YAS537_MAG_AVERAGE_32_MASK);
  1178. if (ret)
  1179. return ret;
  1180. /* Perform the "rcoil" part but skip the "last_after_rcoil" read */
  1181. ret = regmap_write(yas5xx->map, YAS537_CONFIG, BIT(3));
  1182. if (ret)
  1183. return ret;
  1184. /* Wait until the coil has ramped up */
  1185. usleep_range(YAS537_MAG_RCOIL_TIME_US, YAS537_MAG_RCOIL_TIME_US + 100);
  1186. return 0;
  1187. }
  1188. static const struct yas5xx_chip_info yas5xx_chip_info_tbl[] = {
  1189. [yas530] = {
  1190. .devid = YAS530_DEVICE_ID,
  1191. .product_name = "YAS530 MS-3E",
  1192. .version_names = { "A", "B" },
  1193. .volatile_reg = yas530_volatile_reg,
  1194. .volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg),
  1195. .scaling_val2 = 100000000, /* picotesla to Gauss */
  1196. .t_ref = 182, /* counts */
  1197. .min_temp_x10 = -620, /* 1/10:s degrees Celsius */
  1198. .get_measure = yas530_get_measure,
  1199. .get_calibration_data = yas530_get_calibration_data,
  1200. .dump_calibration = yas530_dump_calibration,
  1201. .measure_offsets = yas530_measure_offsets,
  1202. .power_on = yas530_power_on,
  1203. },
  1204. [yas532] = {
  1205. .devid = YAS532_DEVICE_ID,
  1206. .product_name = "YAS532 MS-3R",
  1207. .version_names = { "AB", "AC" },
  1208. .volatile_reg = yas530_volatile_reg,
  1209. .volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg),
  1210. .scaling_val2 = 100000, /* nanotesla to Gauss */
  1211. .t_ref = 390, /* counts */
  1212. .min_temp_x10 = -500, /* 1/10:s degrees Celsius */
  1213. .get_measure = yas530_get_measure,
  1214. .get_calibration_data = yas532_get_calibration_data,
  1215. .dump_calibration = yas530_dump_calibration,
  1216. .measure_offsets = yas530_measure_offsets,
  1217. .power_on = yas530_power_on,
  1218. },
  1219. [yas533] = {
  1220. .devid = YAS532_DEVICE_ID,
  1221. .product_name = "YAS533 MS-3F",
  1222. .version_names = { "AB", "AC" },
  1223. .volatile_reg = yas530_volatile_reg,
  1224. .volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg),
  1225. .scaling_val2 = 100000, /* nanotesla to Gauss */
  1226. .t_ref = 390, /* counts */
  1227. .min_temp_x10 = -500, /* 1/10:s degrees Celsius */
  1228. .get_measure = yas530_get_measure,
  1229. .get_calibration_data = yas532_get_calibration_data,
  1230. .dump_calibration = yas530_dump_calibration,
  1231. .measure_offsets = yas530_measure_offsets,
  1232. .power_on = yas530_power_on,
  1233. },
  1234. [yas537] = {
  1235. .devid = YAS537_DEVICE_ID,
  1236. .product_name = "YAS537 MS-3T",
  1237. .version_names = { "v0", "v1" }, /* version naming unknown */
  1238. .volatile_reg = yas537_volatile_reg,
  1239. .volatile_reg_qty = ARRAY_SIZE(yas537_volatile_reg),
  1240. .scaling_val2 = 100000, /* nanotesla to Gauss */
  1241. .t_ref = 8120, /* counts */
  1242. .min_temp_x10 = -3860, /* 1/10:s degrees Celsius */
  1243. .get_measure = yas537_get_measure,
  1244. .get_calibration_data = yas537_get_calibration_data,
  1245. .dump_calibration = yas537_dump_calibration,
  1246. /* .measure_offets is not needed for yas537 */
  1247. .power_on = yas537_power_on,
  1248. },
  1249. };
  1250. static int yas5xx_probe(struct i2c_client *i2c,
  1251. const struct i2c_device_id *id)
  1252. {
  1253. struct iio_dev *indio_dev;
  1254. struct device *dev = &i2c->dev;
  1255. struct yas5xx *yas5xx;
  1256. const struct yas5xx_chip_info *ci;
  1257. int id_check;
  1258. int ret;
  1259. indio_dev = devm_iio_device_alloc(dev, sizeof(*yas5xx));
  1260. if (!indio_dev)
  1261. return -ENOMEM;
  1262. yas5xx = iio_priv(indio_dev);
  1263. i2c_set_clientdata(i2c, indio_dev);
  1264. yas5xx->dev = dev;
  1265. mutex_init(&yas5xx->lock);
  1266. ret = iio_read_mount_matrix(dev, &yas5xx->orientation);
  1267. if (ret)
  1268. return ret;
  1269. yas5xx->regs[0].supply = "vdd";
  1270. yas5xx->regs[1].supply = "iovdd";
  1271. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(yas5xx->regs),
  1272. yas5xx->regs);
  1273. if (ret)
  1274. return dev_err_probe(dev, ret, "cannot get regulators\n");
  1275. ret = regulator_bulk_enable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
  1276. if (ret)
  1277. return dev_err_probe(dev, ret, "cannot enable regulators\n");
  1278. /* See comment in runtime resume callback */
  1279. usleep_range(31000, 40000);
  1280. /* This will take the device out of reset if need be */
  1281. yas5xx->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  1282. if (IS_ERR(yas5xx->reset)) {
  1283. ret = dev_err_probe(dev, PTR_ERR(yas5xx->reset), "failed to get reset line\n");
  1284. goto reg_off;
  1285. }
  1286. yas5xx->map = devm_regmap_init_i2c(i2c, &yas5xx_regmap_config);
  1287. if (IS_ERR(yas5xx->map)) {
  1288. ret = dev_err_probe(dev, PTR_ERR(yas5xx->map), "failed to allocate register map\n");
  1289. goto assert_reset;
  1290. }
  1291. ci = device_get_match_data(dev);
  1292. if (!ci)
  1293. ci = (const struct yas5xx_chip_info *)id->driver_data;
  1294. yas5xx->chip_info = ci;
  1295. ret = regmap_read(yas5xx->map, YAS5XX_DEVICE_ID, &id_check);
  1296. if (ret)
  1297. goto assert_reset;
  1298. if (id_check != ci->devid) {
  1299. ret = dev_err_probe(dev, -ENODEV,
  1300. "device ID %02x doesn't match %s\n",
  1301. id_check, id->name);
  1302. goto assert_reset;
  1303. }
  1304. ret = ci->get_calibration_data(yas5xx);
  1305. if (ret)
  1306. goto assert_reset;
  1307. dev_info(dev, "detected %s %s\n", ci->product_name,
  1308. ci->version_names[yas5xx->version]);
  1309. ci->dump_calibration(yas5xx);
  1310. ret = ci->power_on(yas5xx);
  1311. if (ret)
  1312. goto assert_reset;
  1313. if (ci->measure_offsets) {
  1314. ret = ci->measure_offsets(yas5xx);
  1315. if (ret)
  1316. goto assert_reset;
  1317. }
  1318. indio_dev->info = &yas5xx_info;
  1319. indio_dev->available_scan_masks = yas5xx_scan_masks;
  1320. indio_dev->modes = INDIO_DIRECT_MODE;
  1321. indio_dev->name = id->name;
  1322. indio_dev->channels = yas5xx_channels;
  1323. indio_dev->num_channels = ARRAY_SIZE(yas5xx_channels);
  1324. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  1325. yas5xx_handle_trigger,
  1326. NULL);
  1327. if (ret) {
  1328. dev_err_probe(dev, ret, "triggered buffer setup failed\n");
  1329. goto assert_reset;
  1330. }
  1331. ret = iio_device_register(indio_dev);
  1332. if (ret) {
  1333. dev_err_probe(dev, ret, "device register failed\n");
  1334. goto cleanup_buffer;
  1335. }
  1336. /* Take runtime PM online */
  1337. pm_runtime_get_noresume(dev);
  1338. pm_runtime_set_active(dev);
  1339. pm_runtime_enable(dev);
  1340. pm_runtime_set_autosuspend_delay(dev, YAS5XX_AUTOSUSPEND_DELAY_MS);
  1341. pm_runtime_use_autosuspend(dev);
  1342. pm_runtime_put(dev);
  1343. return 0;
  1344. cleanup_buffer:
  1345. iio_triggered_buffer_cleanup(indio_dev);
  1346. assert_reset:
  1347. gpiod_set_value_cansleep(yas5xx->reset, 1);
  1348. reg_off:
  1349. regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
  1350. return ret;
  1351. }
  1352. static void yas5xx_remove(struct i2c_client *i2c)
  1353. {
  1354. struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
  1355. struct yas5xx *yas5xx = iio_priv(indio_dev);
  1356. struct device *dev = &i2c->dev;
  1357. iio_device_unregister(indio_dev);
  1358. iio_triggered_buffer_cleanup(indio_dev);
  1359. /*
  1360. * Now we can't get any more reads from the device, which would
  1361. * also call pm_runtime* functions and race with our disable
  1362. * code. Disable PM runtime in orderly fashion and power down.
  1363. */
  1364. pm_runtime_get_sync(dev);
  1365. pm_runtime_put_noidle(dev);
  1366. pm_runtime_disable(dev);
  1367. gpiod_set_value_cansleep(yas5xx->reset, 1);
  1368. regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
  1369. }
  1370. static int yas5xx_runtime_suspend(struct device *dev)
  1371. {
  1372. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1373. struct yas5xx *yas5xx = iio_priv(indio_dev);
  1374. gpiod_set_value_cansleep(yas5xx->reset, 1);
  1375. regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
  1376. return 0;
  1377. }
  1378. static int yas5xx_runtime_resume(struct device *dev)
  1379. {
  1380. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  1381. struct yas5xx *yas5xx = iio_priv(indio_dev);
  1382. const struct yas5xx_chip_info *ci = yas5xx->chip_info;
  1383. int ret;
  1384. ret = regulator_bulk_enable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
  1385. if (ret) {
  1386. dev_err(dev, "cannot enable regulators\n");
  1387. return ret;
  1388. }
  1389. /*
  1390. * The YAS530 datasheet says TVSKW is up to 30 ms, after that 1 ms
  1391. * for all voltages to settle. The YAS532 is 10ms then 4ms for the
  1392. * I2C to come online. Let's keep it safe and put this at 31ms.
  1393. */
  1394. usleep_range(31000, 40000);
  1395. gpiod_set_value_cansleep(yas5xx->reset, 0);
  1396. ret = ci->power_on(yas5xx);
  1397. if (ret) {
  1398. dev_err(dev, "cannot power on\n");
  1399. goto out_reset;
  1400. }
  1401. return 0;
  1402. out_reset:
  1403. gpiod_set_value_cansleep(yas5xx->reset, 1);
  1404. regulator_bulk_disable(ARRAY_SIZE(yas5xx->regs), yas5xx->regs);
  1405. return ret;
  1406. }
  1407. static DEFINE_RUNTIME_DEV_PM_OPS(yas5xx_dev_pm_ops, yas5xx_runtime_suspend,
  1408. yas5xx_runtime_resume, NULL);
  1409. static const struct i2c_device_id yas5xx_id[] = {
  1410. {"yas530", (kernel_ulong_t)&yas5xx_chip_info_tbl[yas530] },
  1411. {"yas532", (kernel_ulong_t)&yas5xx_chip_info_tbl[yas532] },
  1412. {"yas533", (kernel_ulong_t)&yas5xx_chip_info_tbl[yas533] },
  1413. {"yas537", (kernel_ulong_t)&yas5xx_chip_info_tbl[yas537] },
  1414. {}
  1415. };
  1416. MODULE_DEVICE_TABLE(i2c, yas5xx_id);
  1417. static const struct of_device_id yas5xx_of_match[] = {
  1418. { .compatible = "yamaha,yas530", &yas5xx_chip_info_tbl[yas530] },
  1419. { .compatible = "yamaha,yas532", &yas5xx_chip_info_tbl[yas532] },
  1420. { .compatible = "yamaha,yas533", &yas5xx_chip_info_tbl[yas533] },
  1421. { .compatible = "yamaha,yas537", &yas5xx_chip_info_tbl[yas537] },
  1422. {}
  1423. };
  1424. MODULE_DEVICE_TABLE(of, yas5xx_of_match);
  1425. static struct i2c_driver yas5xx_driver = {
  1426. .driver = {
  1427. .name = "yas5xx",
  1428. .of_match_table = yas5xx_of_match,
  1429. .pm = pm_ptr(&yas5xx_dev_pm_ops),
  1430. },
  1431. .probe = yas5xx_probe,
  1432. .remove = yas5xx_remove,
  1433. .id_table = yas5xx_id,
  1434. };
  1435. module_i2c_driver(yas5xx_driver);
  1436. MODULE_DESCRIPTION("Yamaha YAS53x 3-axis magnetometer driver");
  1437. MODULE_AUTHOR("Linus Walleij");
  1438. MODULE_LICENSE("GPL v2");