zpa2326.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  1. /*
  2. * Murata ZPA2326 pressure and temperature sensor IIO driver
  3. *
  4. * Copyright (c) 2016 Parrot S.A.
  5. *
  6. * Author: Gregor Boirie <gregor.boirie@parrot.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. */
  17. /**
  18. * DOC: ZPA2326 theory of operations
  19. *
  20. * This driver supports %INDIO_DIRECT_MODE and %INDIO_BUFFER_TRIGGERED IIO
  21. * modes.
  22. * A internal hardware trigger is also implemented to dispatch registered IIO
  23. * trigger consumers upon "sample ready" interrupts.
  24. *
  25. * ZPA2326 hardware supports 2 sampling mode: one shot and continuous.
  26. *
  27. * A complete one shot sampling cycle gets device out of low power mode,
  28. * performs pressure and temperature measurements, then automatically switches
  29. * back to low power mode. It is meant for on demand sampling with optimal power
  30. * saving at the cost of lower sampling rate and higher software overhead.
  31. * This is a natural candidate for IIO read_raw hook implementation
  32. * (%INDIO_DIRECT_MODE). It is also used for triggered buffering support to
  33. * ensure explicit synchronization with external trigger events
  34. * (%INDIO_BUFFER_TRIGGERED).
  35. *
  36. * The continuous mode works according to a periodic hardware measurement
  37. * process continuously pushing samples into an internal hardware FIFO (for
  38. * pressure samples only). Measurement cycle completion may be signaled by a
  39. * "sample ready" interrupt.
  40. * Typical software sequence of operations :
  41. * - get device out of low power mode,
  42. * - setup hardware sampling period,
  43. * - at end of period, upon data ready interrupt: pop pressure samples out of
  44. * hardware FIFO and fetch temperature sample
  45. * - when no longer needed, stop sampling process by putting device into
  46. * low power mode.
  47. * This mode is used to implement %INDIO_BUFFER_TRIGGERED mode if device tree
  48. * declares a valid interrupt line. In this case, the internal hardware trigger
  49. * drives acquisition.
  50. *
  51. * Note that hardware sampling frequency is taken into account only when
  52. * internal hardware trigger is attached as the highest sampling rate seems to
  53. * be the most energy efficient.
  54. *
  55. * TODO:
  56. * preset pressure threshold crossing / IIO events ;
  57. * differential pressure sampling ;
  58. * hardware samples averaging.
  59. */
  60. #include <linux/module.h>
  61. #include <linux/kernel.h>
  62. #include <linux/delay.h>
  63. #include <linux/interrupt.h>
  64. #include <linux/regulator/consumer.h>
  65. #include <linux/pm_runtime.h>
  66. #include <linux/regmap.h>
  67. #include <linux/iio/iio.h>
  68. #include <linux/iio/sysfs.h>
  69. #include <linux/iio/buffer.h>
  70. #include <linux/iio/trigger.h>
  71. #include <linux/iio/trigger_consumer.h>
  72. #include <linux/iio/triggered_buffer.h>
  73. #include "zpa2326.h"
  74. /* 200 ms should be enough for the longest conversion time in one-shot mode. */
  75. #define ZPA2326_CONVERSION_JIFFIES (HZ / 5)
  76. /* There should be a 1 ms delay (Tpup) after getting out of reset. */
  77. #define ZPA2326_TPUP_USEC_MIN (1000)
  78. #define ZPA2326_TPUP_USEC_MAX (2000)
  79. /**
  80. * struct zpa2326_frequency - Hardware sampling frequency descriptor
  81. * @hz : Frequency in Hertz.
  82. * @odr: Output Data Rate word as expected by %ZPA2326_CTRL_REG3_REG.
  83. */
  84. struct zpa2326_frequency {
  85. int hz;
  86. u16 odr;
  87. };
  88. /*
  89. * Keep these in strict ascending order: last array entry is expected to
  90. * correspond to the highest sampling frequency.
  91. */
  92. static const struct zpa2326_frequency zpa2326_sampling_frequencies[] = {
  93. { .hz = 1, .odr = 1 << ZPA2326_CTRL_REG3_ODR_SHIFT },
  94. { .hz = 5, .odr = 5 << ZPA2326_CTRL_REG3_ODR_SHIFT },
  95. { .hz = 11, .odr = 6 << ZPA2326_CTRL_REG3_ODR_SHIFT },
  96. { .hz = 23, .odr = 7 << ZPA2326_CTRL_REG3_ODR_SHIFT },
  97. };
  98. /* Return the highest hardware sampling frequency available. */
  99. static const struct zpa2326_frequency *zpa2326_highest_frequency(void)
  100. {
  101. return &zpa2326_sampling_frequencies[
  102. ARRAY_SIZE(zpa2326_sampling_frequencies) - 1];
  103. }
  104. /**
  105. * struct zpa_private - Per-device internal private state
  106. * @timestamp: Buffered samples ready datum.
  107. * @regmap: Underlying I2C / SPI bus adapter used to abstract slave register
  108. * accesses.
  109. * @result: Allows sampling logic to get completion status of operations
  110. * that interrupt handlers perform asynchronously.
  111. * @data_ready: Interrupt handler uses this to wake user context up at sampling
  112. * operation completion.
  113. * @trigger: Optional hardware / interrupt driven trigger used to notify
  114. * external devices a new sample is ready.
  115. * @waken: Flag indicating whether or not device has just been powered on.
  116. * @irq: Optional interrupt line: negative or zero if not declared into
  117. * DT, in which case sampling logic keeps polling status register
  118. * to detect completion.
  119. * @frequency: Current hardware sampling frequency.
  120. * @vref: Power / voltage reference.
  121. * @vdd: Power supply.
  122. */
  123. struct zpa2326_private {
  124. s64 timestamp;
  125. struct regmap *regmap;
  126. int result;
  127. struct completion data_ready;
  128. struct iio_trigger *trigger;
  129. bool waken;
  130. int irq;
  131. const struct zpa2326_frequency *frequency;
  132. struct regulator *vref;
  133. struct regulator *vdd;
  134. };
  135. #define zpa2326_err(idev, fmt, ...) \
  136. dev_err(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
  137. #define zpa2326_warn(idev, fmt, ...) \
  138. dev_warn(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
  139. #define zpa2326_dbg(idev, fmt, ...) \
  140. dev_dbg(idev->dev.parent, fmt "\n", ##__VA_ARGS__)
  141. bool zpa2326_isreg_writeable(struct device *dev, unsigned int reg)
  142. {
  143. switch (reg) {
  144. case ZPA2326_REF_P_XL_REG:
  145. case ZPA2326_REF_P_L_REG:
  146. case ZPA2326_REF_P_H_REG:
  147. case ZPA2326_RES_CONF_REG:
  148. case ZPA2326_CTRL_REG0_REG:
  149. case ZPA2326_CTRL_REG1_REG:
  150. case ZPA2326_CTRL_REG2_REG:
  151. case ZPA2326_CTRL_REG3_REG:
  152. case ZPA2326_THS_P_LOW_REG:
  153. case ZPA2326_THS_P_HIGH_REG:
  154. return true;
  155. default:
  156. return false;
  157. }
  158. }
  159. EXPORT_SYMBOL_GPL(zpa2326_isreg_writeable);
  160. bool zpa2326_isreg_readable(struct device *dev, unsigned int reg)
  161. {
  162. switch (reg) {
  163. case ZPA2326_REF_P_XL_REG:
  164. case ZPA2326_REF_P_L_REG:
  165. case ZPA2326_REF_P_H_REG:
  166. case ZPA2326_DEVICE_ID_REG:
  167. case ZPA2326_RES_CONF_REG:
  168. case ZPA2326_CTRL_REG0_REG:
  169. case ZPA2326_CTRL_REG1_REG:
  170. case ZPA2326_CTRL_REG2_REG:
  171. case ZPA2326_CTRL_REG3_REG:
  172. case ZPA2326_INT_SOURCE_REG:
  173. case ZPA2326_THS_P_LOW_REG:
  174. case ZPA2326_THS_P_HIGH_REG:
  175. case ZPA2326_STATUS_REG:
  176. case ZPA2326_PRESS_OUT_XL_REG:
  177. case ZPA2326_PRESS_OUT_L_REG:
  178. case ZPA2326_PRESS_OUT_H_REG:
  179. case ZPA2326_TEMP_OUT_L_REG:
  180. case ZPA2326_TEMP_OUT_H_REG:
  181. return true;
  182. default:
  183. return false;
  184. }
  185. }
  186. EXPORT_SYMBOL_GPL(zpa2326_isreg_readable);
  187. bool zpa2326_isreg_precious(struct device *dev, unsigned int reg)
  188. {
  189. switch (reg) {
  190. case ZPA2326_INT_SOURCE_REG:
  191. case ZPA2326_PRESS_OUT_H_REG:
  192. return true;
  193. default:
  194. return false;
  195. }
  196. }
  197. EXPORT_SYMBOL_GPL(zpa2326_isreg_precious);
  198. /**
  199. * zpa2326_enable_device() - Enable device, i.e. get out of low power mode.
  200. * @indio_dev: The IIO device associated with the hardware to enable.
  201. *
  202. * Required to access complete register space and to perform any sampling
  203. * or control operations.
  204. *
  205. * Return: Zero when successful, a negative error code otherwise.
  206. */
  207. static int zpa2326_enable_device(const struct iio_dev *indio_dev)
  208. {
  209. int err;
  210. err = regmap_write(((struct zpa2326_private *)
  211. iio_priv(indio_dev))->regmap,
  212. ZPA2326_CTRL_REG0_REG, ZPA2326_CTRL_REG0_ENABLE);
  213. if (err) {
  214. zpa2326_err(indio_dev, "failed to enable device (%d)", err);
  215. return err;
  216. }
  217. zpa2326_dbg(indio_dev, "enabled");
  218. return 0;
  219. }
  220. /**
  221. * zpa2326_sleep() - Disable device, i.e. switch to low power mode.
  222. * @indio_dev: The IIO device associated with the hardware to disable.
  223. *
  224. * Only %ZPA2326_DEVICE_ID_REG and %ZPA2326_CTRL_REG0_REG registers may be
  225. * accessed once device is in the disabled state.
  226. *
  227. * Return: Zero when successful, a negative error code otherwise.
  228. */
  229. static int zpa2326_sleep(const struct iio_dev *indio_dev)
  230. {
  231. int err;
  232. err = regmap_write(((struct zpa2326_private *)
  233. iio_priv(indio_dev))->regmap,
  234. ZPA2326_CTRL_REG0_REG, 0);
  235. if (err) {
  236. zpa2326_err(indio_dev, "failed to sleep (%d)", err);
  237. return err;
  238. }
  239. zpa2326_dbg(indio_dev, "sleeping");
  240. return 0;
  241. }
  242. /**
  243. * zpa2326_reset_device() - Reset device to default hardware state.
  244. * @indio_dev: The IIO device associated with the hardware to reset.
  245. *
  246. * Disable sampling and empty hardware FIFO.
  247. * Device must be enabled before reset, i.e. not in low power mode.
  248. *
  249. * Return: Zero when successful, a negative error code otherwise.
  250. */
  251. static int zpa2326_reset_device(const struct iio_dev *indio_dev)
  252. {
  253. int err;
  254. err = regmap_write(((struct zpa2326_private *)
  255. iio_priv(indio_dev))->regmap,
  256. ZPA2326_CTRL_REG2_REG, ZPA2326_CTRL_REG2_SWRESET);
  257. if (err) {
  258. zpa2326_err(indio_dev, "failed to reset device (%d)", err);
  259. return err;
  260. }
  261. usleep_range(ZPA2326_TPUP_USEC_MIN, ZPA2326_TPUP_USEC_MAX);
  262. zpa2326_dbg(indio_dev, "reset");
  263. return 0;
  264. }
  265. /**
  266. * zpa2326_start_oneshot() - Start a single sampling cycle, i.e. in one shot
  267. * mode.
  268. * @indio_dev: The IIO device associated with the sampling hardware.
  269. *
  270. * Device must have been previously enabled and configured for one shot mode.
  271. * Device will be switched back to low power mode at end of cycle.
  272. *
  273. * Return: Zero when successful, a negative error code otherwise.
  274. */
  275. static int zpa2326_start_oneshot(const struct iio_dev *indio_dev)
  276. {
  277. int err;
  278. err = regmap_write(((struct zpa2326_private *)
  279. iio_priv(indio_dev))->regmap,
  280. ZPA2326_CTRL_REG0_REG,
  281. ZPA2326_CTRL_REG0_ENABLE |
  282. ZPA2326_CTRL_REG0_ONE_SHOT);
  283. if (err) {
  284. zpa2326_err(indio_dev, "failed to start one shot cycle (%d)",
  285. err);
  286. return err;
  287. }
  288. zpa2326_dbg(indio_dev, "one shot cycle started");
  289. return 0;
  290. }
  291. /**
  292. * zpa2326_power_on() - Power on device to allow subsequent configuration.
  293. * @indio_dev: The IIO device associated with the sampling hardware.
  294. * @private: Internal private state related to @indio_dev.
  295. *
  296. * Sampling will be disabled, preventing strange things from happening in our
  297. * back. Hardware FIFO content will be cleared.
  298. * When successful, device will be left in the enabled state to allow further
  299. * configuration.
  300. *
  301. * Return: Zero when successful, a negative error code otherwise.
  302. */
  303. static int zpa2326_power_on(const struct iio_dev *indio_dev,
  304. const struct zpa2326_private *private)
  305. {
  306. int err;
  307. err = regulator_enable(private->vref);
  308. if (err)
  309. return err;
  310. err = regulator_enable(private->vdd);
  311. if (err)
  312. goto vref;
  313. zpa2326_dbg(indio_dev, "powered on");
  314. err = zpa2326_enable_device(indio_dev);
  315. if (err)
  316. goto vdd;
  317. err = zpa2326_reset_device(indio_dev);
  318. if (err)
  319. goto sleep;
  320. return 0;
  321. sleep:
  322. zpa2326_sleep(indio_dev);
  323. vdd:
  324. regulator_disable(private->vdd);
  325. vref:
  326. regulator_disable(private->vref);
  327. zpa2326_dbg(indio_dev, "powered off");
  328. return err;
  329. }
  330. /**
  331. * zpa2326_power_off() - Power off device, i.e. disable attached power
  332. * regulators.
  333. * @indio_dev: The IIO device associated with the sampling hardware.
  334. * @private: Internal private state related to @indio_dev.
  335. *
  336. * Return: Zero when successful, a negative error code otherwise.
  337. */
  338. static void zpa2326_power_off(const struct iio_dev *indio_dev,
  339. const struct zpa2326_private *private)
  340. {
  341. regulator_disable(private->vdd);
  342. regulator_disable(private->vref);
  343. zpa2326_dbg(indio_dev, "powered off");
  344. }
  345. /**
  346. * zpa2326_config_oneshot() - Setup device for one shot / on demand mode.
  347. * @indio_dev: The IIO device associated with the sampling hardware.
  348. * @irq: Optional interrupt line the hardware uses to notify new data
  349. * samples are ready. Negative or zero values indicate no interrupts
  350. * are available, meaning polling is required.
  351. *
  352. * Output Data Rate is configured for the highest possible rate so that
  353. * conversion time and power consumption are reduced to a minimum.
  354. * Note that hardware internal averaging machinery (not implemented in this
  355. * driver) is not applicable in this mode.
  356. *
  357. * Device must have been previously enabled before calling
  358. * zpa2326_config_oneshot().
  359. *
  360. * Return: Zero when successful, a negative error code otherwise.
  361. */
  362. static int zpa2326_config_oneshot(const struct iio_dev *indio_dev,
  363. int irq)
  364. {
  365. struct regmap *regs = ((struct zpa2326_private *)
  366. iio_priv(indio_dev))->regmap;
  367. const struct zpa2326_frequency *freq = zpa2326_highest_frequency();
  368. int err;
  369. /* Setup highest available Output Data Rate for one shot mode. */
  370. err = regmap_write(regs, ZPA2326_CTRL_REG3_REG, freq->odr);
  371. if (err)
  372. return err;
  373. if (irq > 0) {
  374. /* Request interrupt when new sample is available. */
  375. err = regmap_write(regs, ZPA2326_CTRL_REG1_REG,
  376. (u8)~ZPA2326_CTRL_REG1_MASK_DATA_READY);
  377. if (err) {
  378. dev_err(indio_dev->dev.parent,
  379. "failed to setup one shot mode (%d)", err);
  380. return err;
  381. }
  382. }
  383. zpa2326_dbg(indio_dev, "one shot mode setup @%dHz", freq->hz);
  384. return 0;
  385. }
  386. /**
  387. * zpa2326_clear_fifo() - Clear remaining entries in hardware FIFO.
  388. * @indio_dev: The IIO device associated with the sampling hardware.
  389. * @min_count: Number of samples present within hardware FIFO.
  390. *
  391. * @min_count argument is a hint corresponding to the known minimum number of
  392. * samples currently living in the FIFO. This allows to reduce the number of bus
  393. * accesses by skipping status register read operation as long as we know for
  394. * sure there are still entries left.
  395. *
  396. * Return: Zero when successful, a negative error code otherwise.
  397. */
  398. static int zpa2326_clear_fifo(const struct iio_dev *indio_dev,
  399. unsigned int min_count)
  400. {
  401. struct regmap *regs = ((struct zpa2326_private *)
  402. iio_priv(indio_dev))->regmap;
  403. int err;
  404. unsigned int val;
  405. if (!min_count) {
  406. /*
  407. * No hint: read status register to determine whether FIFO is
  408. * empty or not.
  409. */
  410. err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
  411. if (err < 0)
  412. goto err;
  413. if (val & ZPA2326_STATUS_FIFO_E)
  414. /* Fifo is empty: nothing to trash. */
  415. return 0;
  416. }
  417. /* Clear FIFO. */
  418. do {
  419. /*
  420. * A single fetch from pressure MSB register is enough to pop
  421. * values out of FIFO.
  422. */
  423. err = regmap_read(regs, ZPA2326_PRESS_OUT_H_REG, &val);
  424. if (err < 0)
  425. goto err;
  426. if (min_count) {
  427. /*
  428. * We know for sure there are at least min_count entries
  429. * left in FIFO. Skip status register read.
  430. */
  431. min_count--;
  432. continue;
  433. }
  434. err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
  435. if (err < 0)
  436. goto err;
  437. } while (!(val & ZPA2326_STATUS_FIFO_E));
  438. zpa2326_dbg(indio_dev, "FIFO cleared");
  439. return 0;
  440. err:
  441. zpa2326_err(indio_dev, "failed to clear FIFO (%d)", err);
  442. return err;
  443. }
  444. /**
  445. * zpa2326_dequeue_pressure() - Retrieve the most recent pressure sample from
  446. * hardware FIFO.
  447. * @indio_dev: The IIO device associated with the sampling hardware.
  448. * @pressure: Sampled pressure output.
  449. *
  450. * Note that ZPA2326 hardware FIFO stores pressure samples only.
  451. *
  452. * Return: Zero when successful, a negative error code otherwise.
  453. */
  454. static int zpa2326_dequeue_pressure(const struct iio_dev *indio_dev,
  455. u32 *pressure)
  456. {
  457. struct regmap *regs = ((struct zpa2326_private *)
  458. iio_priv(indio_dev))->regmap;
  459. unsigned int val;
  460. int err;
  461. int cleared = -1;
  462. err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
  463. if (err < 0)
  464. return err;
  465. *pressure = 0;
  466. if (val & ZPA2326_STATUS_P_OR) {
  467. /*
  468. * Fifo overrun : first sample dequeued from FIFO is the
  469. * newest.
  470. */
  471. zpa2326_warn(indio_dev, "FIFO overflow");
  472. err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
  473. 3);
  474. if (err)
  475. return err;
  476. #define ZPA2326_FIFO_DEPTH (16U)
  477. /* Hardware FIFO may hold no more than 16 pressure samples. */
  478. return zpa2326_clear_fifo(indio_dev, ZPA2326_FIFO_DEPTH - 1);
  479. }
  480. /*
  481. * Fifo has not overflown : retrieve newest sample. We need to pop
  482. * values out until FIFO is empty : last fetched pressure is the newest.
  483. * In nominal cases, we should find a single queued sample only.
  484. */
  485. do {
  486. err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
  487. 3);
  488. if (err)
  489. return err;
  490. err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
  491. if (err < 0)
  492. return err;
  493. cleared++;
  494. } while (!(val & ZPA2326_STATUS_FIFO_E));
  495. if (cleared)
  496. /*
  497. * Samples were pushed by hardware during previous rounds but we
  498. * didn't consume them fast enough: inform user.
  499. */
  500. zpa2326_dbg(indio_dev, "cleared %d FIFO entries", cleared);
  501. return 0;
  502. }
  503. /**
  504. * zpa2326_fill_sample_buffer() - Enqueue new channel samples to IIO buffer.
  505. * @indio_dev: The IIO device associated with the sampling hardware.
  506. * @private: Internal private state related to @indio_dev.
  507. *
  508. * Return: Zero when successful, a negative error code otherwise.
  509. */
  510. static int zpa2326_fill_sample_buffer(struct iio_dev *indio_dev,
  511. const struct zpa2326_private *private)
  512. {
  513. struct {
  514. u32 pressure;
  515. u16 temperature;
  516. u64 timestamp;
  517. } sample;
  518. int err;
  519. if (test_bit(0, indio_dev->active_scan_mask)) {
  520. /* Get current pressure from hardware FIFO. */
  521. err = zpa2326_dequeue_pressure(indio_dev, &sample.pressure);
  522. if (err) {
  523. zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
  524. err);
  525. return err;
  526. }
  527. }
  528. if (test_bit(1, indio_dev->active_scan_mask)) {
  529. /* Get current temperature. */
  530. err = regmap_bulk_read(private->regmap, ZPA2326_TEMP_OUT_L_REG,
  531. &sample.temperature, 2);
  532. if (err) {
  533. zpa2326_warn(indio_dev,
  534. "failed to fetch temperature (%d)", err);
  535. return err;
  536. }
  537. }
  538. /*
  539. * Now push samples using timestamp stored either :
  540. * - by hardware interrupt handler if interrupt is available: see
  541. * zpa2326_handle_irq(),
  542. * - or oneshot completion polling machinery : see
  543. * zpa2326_trigger_handler().
  544. */
  545. zpa2326_dbg(indio_dev, "filling raw samples buffer");
  546. iio_push_to_buffers_with_timestamp(indio_dev, &sample,
  547. private->timestamp);
  548. return 0;
  549. }
  550. #ifdef CONFIG_PM
  551. static int zpa2326_runtime_suspend(struct device *parent)
  552. {
  553. const struct iio_dev *indio_dev = dev_get_drvdata(parent);
  554. if (pm_runtime_autosuspend_expiration(parent))
  555. /* Userspace changed autosuspend delay. */
  556. return -EAGAIN;
  557. zpa2326_power_off(indio_dev, iio_priv(indio_dev));
  558. return 0;
  559. }
  560. static int zpa2326_runtime_resume(struct device *parent)
  561. {
  562. const struct iio_dev *indio_dev = dev_get_drvdata(parent);
  563. return zpa2326_power_on(indio_dev, iio_priv(indio_dev));
  564. }
  565. const struct dev_pm_ops zpa2326_pm_ops = {
  566. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  567. pm_runtime_force_resume)
  568. SET_RUNTIME_PM_OPS(zpa2326_runtime_suspend, zpa2326_runtime_resume,
  569. NULL)
  570. };
  571. EXPORT_SYMBOL_GPL(zpa2326_pm_ops);
  572. /**
  573. * zpa2326_resume() - Request the PM layer to power supply the device.
  574. * @indio_dev: The IIO device associated with the sampling hardware.
  575. *
  576. * Return:
  577. * < 0 - a negative error code meaning failure ;
  578. * 0 - success, device has just been powered up ;
  579. * 1 - success, device was already powered.
  580. */
  581. static int zpa2326_resume(const struct iio_dev *indio_dev)
  582. {
  583. int err;
  584. err = pm_runtime_get_sync(indio_dev->dev.parent);
  585. if (err < 0)
  586. return err;
  587. if (err > 0) {
  588. /*
  589. * Device was already power supplied: get it out of low power
  590. * mode and inform caller.
  591. */
  592. zpa2326_enable_device(indio_dev);
  593. return 1;
  594. }
  595. /* Inform caller device has just been brought back to life. */
  596. return 0;
  597. }
  598. /**
  599. * zpa2326_suspend() - Schedule a power down using autosuspend feature of PM
  600. * layer.
  601. * @indio_dev: The IIO device associated with the sampling hardware.
  602. *
  603. * Device is switched to low power mode at first to save power even when
  604. * attached regulator is a "dummy" one.
  605. */
  606. static void zpa2326_suspend(struct iio_dev *indio_dev)
  607. {
  608. struct device *parent = indio_dev->dev.parent;
  609. zpa2326_sleep(indio_dev);
  610. pm_runtime_mark_last_busy(parent);
  611. pm_runtime_put_autosuspend(parent);
  612. }
  613. static void zpa2326_init_runtime(struct device *parent)
  614. {
  615. pm_runtime_get_noresume(parent);
  616. pm_runtime_set_active(parent);
  617. pm_runtime_enable(parent);
  618. pm_runtime_set_autosuspend_delay(parent, 1000);
  619. pm_runtime_use_autosuspend(parent);
  620. pm_runtime_mark_last_busy(parent);
  621. pm_runtime_put_autosuspend(parent);
  622. }
  623. static void zpa2326_fini_runtime(struct device *parent)
  624. {
  625. pm_runtime_disable(parent);
  626. pm_runtime_set_suspended(parent);
  627. }
  628. #else /* !CONFIG_PM */
  629. static int zpa2326_resume(const struct iio_dev *indio_dev)
  630. {
  631. zpa2326_enable_device(indio_dev);
  632. return 0;
  633. }
  634. static void zpa2326_suspend(struct iio_dev *indio_dev)
  635. {
  636. zpa2326_sleep(indio_dev);
  637. }
  638. #define zpa2326_init_runtime(_parent)
  639. #define zpa2326_fini_runtime(_parent)
  640. #endif /* !CONFIG_PM */
  641. /**
  642. * zpa2326_handle_irq() - Process hardware interrupts.
  643. * @irq: Interrupt line the hardware uses to notify new data has arrived.
  644. * @data: The IIO device associated with the sampling hardware.
  645. *
  646. * Timestamp buffered samples as soon as possible then schedule threaded bottom
  647. * half.
  648. *
  649. * Return: Always successful.
  650. */
  651. static irqreturn_t zpa2326_handle_irq(int irq, void *data)
  652. {
  653. struct iio_dev *indio_dev = data;
  654. if (iio_buffer_enabled(indio_dev)) {
  655. /* Timestamping needed for buffered sampling only. */
  656. ((struct zpa2326_private *)
  657. iio_priv(indio_dev))->timestamp = iio_get_time_ns(indio_dev);
  658. }
  659. return IRQ_WAKE_THREAD;
  660. }
  661. /**
  662. * zpa2326_handle_threaded_irq() - Interrupt bottom-half handler.
  663. * @irq: Interrupt line the hardware uses to notify new data has arrived.
  664. * @data: The IIO device associated with the sampling hardware.
  665. *
  666. * Mainly ensures interrupt is caused by a real "new sample available"
  667. * condition. This relies upon the ability to perform blocking / sleeping bus
  668. * accesses to slave's registers. This is why zpa2326_handle_threaded_irq() is
  669. * called from within a thread, i.e. not called from hard interrupt context.
  670. *
  671. * When device is using its own internal hardware trigger in continuous sampling
  672. * mode, data are available into hardware FIFO once interrupt has occurred. All
  673. * we have to do is to dispatch the trigger, which in turn will fetch data and
  674. * fill IIO buffer.
  675. *
  676. * When not using its own internal hardware trigger, the device has been
  677. * configured in one-shot mode either by an external trigger or the IIO read_raw
  678. * hook. This means one of the latter is currently waiting for sampling
  679. * completion, in which case we must simply wake it up.
  680. *
  681. * See zpa2326_trigger_handler().
  682. *
  683. * Return:
  684. * %IRQ_NONE - no consistent interrupt happened ;
  685. * %IRQ_HANDLED - there was new samples available.
  686. */
  687. static irqreturn_t zpa2326_handle_threaded_irq(int irq, void *data)
  688. {
  689. struct iio_dev *indio_dev = data;
  690. struct zpa2326_private *priv = iio_priv(indio_dev);
  691. unsigned int val;
  692. bool cont;
  693. irqreturn_t ret = IRQ_NONE;
  694. /*
  695. * Are we using our own internal trigger in triggered buffer mode, i.e.,
  696. * currently working in continuous sampling mode ?
  697. */
  698. cont = (iio_buffer_enabled(indio_dev) &&
  699. iio_trigger_using_own(indio_dev));
  700. /*
  701. * Device works according to a level interrupt scheme: reading interrupt
  702. * status de-asserts interrupt line.
  703. */
  704. priv->result = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
  705. if (priv->result < 0) {
  706. if (cont)
  707. return IRQ_NONE;
  708. goto complete;
  709. }
  710. /* Data ready is the only interrupt source we requested. */
  711. if (!(val & ZPA2326_INT_SOURCE_DATA_READY)) {
  712. /*
  713. * Interrupt happened but no new sample available: likely caused
  714. * by spurious interrupts, in which case, returning IRQ_NONE
  715. * allows to benefit from the generic spurious interrupts
  716. * handling.
  717. */
  718. zpa2326_warn(indio_dev, "unexpected interrupt status %02x",
  719. val);
  720. if (cont)
  721. return IRQ_NONE;
  722. priv->result = -ENODATA;
  723. goto complete;
  724. }
  725. /* New sample available: dispatch internal trigger consumers. */
  726. iio_trigger_poll_chained(priv->trigger);
  727. if (cont)
  728. /*
  729. * Internal hardware trigger has been scheduled above : it will
  730. * fetch data on its own.
  731. */
  732. return IRQ_HANDLED;
  733. ret = IRQ_HANDLED;
  734. complete:
  735. /*
  736. * Wake up direct or externaly triggered buffer mode waiters: see
  737. * zpa2326_sample_oneshot() and zpa2326_trigger_handler().
  738. */
  739. complete(&priv->data_ready);
  740. return ret;
  741. }
  742. /**
  743. * zpa2326_wait_oneshot_completion() - Wait for oneshot data ready interrupt.
  744. * @indio_dev: The IIO device associated with the sampling hardware.
  745. * @private: Internal private state related to @indio_dev.
  746. *
  747. * Return: Zero when successful, a negative error code otherwise.
  748. */
  749. static int zpa2326_wait_oneshot_completion(const struct iio_dev *indio_dev,
  750. struct zpa2326_private *private)
  751. {
  752. unsigned int val;
  753. long timeout;
  754. zpa2326_dbg(indio_dev, "waiting for one shot completion interrupt");
  755. timeout = wait_for_completion_interruptible_timeout(
  756. &private->data_ready, ZPA2326_CONVERSION_JIFFIES);
  757. if (timeout > 0)
  758. /*
  759. * Interrupt handler completed before timeout: return operation
  760. * status.
  761. */
  762. return private->result;
  763. /* Clear all interrupts just to be sure. */
  764. regmap_read(private->regmap, ZPA2326_INT_SOURCE_REG, &val);
  765. if (!timeout) {
  766. /* Timed out. */
  767. zpa2326_warn(indio_dev, "no one shot interrupt occurred (%ld)",
  768. timeout);
  769. return -ETIME;
  770. }
  771. zpa2326_warn(indio_dev, "wait for one shot interrupt cancelled");
  772. return -ERESTARTSYS;
  773. }
  774. static int zpa2326_init_managed_irq(struct device *parent,
  775. struct iio_dev *indio_dev,
  776. struct zpa2326_private *private,
  777. int irq)
  778. {
  779. int err;
  780. private->irq = irq;
  781. if (irq <= 0) {
  782. /*
  783. * Platform declared no interrupt line: device will be polled
  784. * for data availability.
  785. */
  786. dev_info(parent, "no interrupt found, running in polling mode");
  787. return 0;
  788. }
  789. init_completion(&private->data_ready);
  790. /* Request handler to be scheduled into threaded interrupt context. */
  791. err = devm_request_threaded_irq(parent, irq, zpa2326_handle_irq,
  792. zpa2326_handle_threaded_irq,
  793. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  794. dev_name(parent), indio_dev);
  795. if (err) {
  796. dev_err(parent, "failed to request interrupt %d (%d)", irq,
  797. err);
  798. return err;
  799. }
  800. dev_info(parent, "using interrupt %d", irq);
  801. return 0;
  802. }
  803. /**
  804. * zpa2326_poll_oneshot_completion() - Actively poll for one shot data ready.
  805. * @indio_dev: The IIO device associated with the sampling hardware.
  806. *
  807. * Loop over registers content to detect end of sampling cycle. Used when DT
  808. * declared no valid interrupt lines.
  809. *
  810. * Return: Zero when successful, a negative error code otherwise.
  811. */
  812. static int zpa2326_poll_oneshot_completion(const struct iio_dev *indio_dev)
  813. {
  814. unsigned long tmout = jiffies + ZPA2326_CONVERSION_JIFFIES;
  815. struct regmap *regs = ((struct zpa2326_private *)
  816. iio_priv(indio_dev))->regmap;
  817. unsigned int val;
  818. int err;
  819. zpa2326_dbg(indio_dev, "polling for one shot completion");
  820. /*
  821. * At least, 100 ms is needed for the device to complete its one-shot
  822. * cycle.
  823. */
  824. if (msleep_interruptible(100))
  825. return -ERESTARTSYS;
  826. /* Poll for conversion completion in hardware. */
  827. while (true) {
  828. err = regmap_read(regs, ZPA2326_CTRL_REG0_REG, &val);
  829. if (err < 0)
  830. goto err;
  831. if (!(val & ZPA2326_CTRL_REG0_ONE_SHOT))
  832. /* One-shot bit self clears at conversion end. */
  833. break;
  834. if (time_after(jiffies, tmout)) {
  835. /* Prevent from waiting forever : let's time out. */
  836. err = -ETIME;
  837. goto err;
  838. }
  839. usleep_range(10000, 20000);
  840. }
  841. /*
  842. * In oneshot mode, pressure sample availability guarantees that
  843. * temperature conversion has also completed : just check pressure
  844. * status bit to keep things simple.
  845. */
  846. err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
  847. if (err < 0)
  848. goto err;
  849. if (!(val & ZPA2326_STATUS_P_DA)) {
  850. /* No sample available. */
  851. err = -ENODATA;
  852. goto err;
  853. }
  854. return 0;
  855. err:
  856. zpa2326_warn(indio_dev, "failed to poll one shot completion (%d)", err);
  857. return err;
  858. }
  859. /**
  860. * zpa2326_fetch_raw_sample() - Retrieve a raw sample and convert it to CPU
  861. * endianness.
  862. * @indio_dev: The IIO device associated with the sampling hardware.
  863. * @type: Type of measurement / channel to fetch from.
  864. * @value: Sample output.
  865. *
  866. * Return: Zero when successful, a negative error code otherwise.
  867. */
  868. static int zpa2326_fetch_raw_sample(const struct iio_dev *indio_dev,
  869. enum iio_chan_type type,
  870. int *value)
  871. {
  872. struct regmap *regs = ((struct zpa2326_private *)
  873. iio_priv(indio_dev))->regmap;
  874. int err;
  875. switch (type) {
  876. case IIO_PRESSURE:
  877. zpa2326_dbg(indio_dev, "fetching raw pressure sample");
  878. err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, value,
  879. 3);
  880. if (err) {
  881. zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
  882. err);
  883. return err;
  884. }
  885. /* Pressure is a 24 bits wide little-endian unsigned int. */
  886. *value = (((u8 *)value)[2] << 16) | (((u8 *)value)[1] << 8) |
  887. ((u8 *)value)[0];
  888. return IIO_VAL_INT;
  889. case IIO_TEMP:
  890. zpa2326_dbg(indio_dev, "fetching raw temperature sample");
  891. err = regmap_bulk_read(regs, ZPA2326_TEMP_OUT_L_REG, value, 2);
  892. if (err) {
  893. zpa2326_warn(indio_dev,
  894. "failed to fetch temperature (%d)", err);
  895. return err;
  896. }
  897. /* Temperature is a 16 bits wide little-endian signed int. */
  898. *value = (int)le16_to_cpup((__le16 *)value);
  899. return IIO_VAL_INT;
  900. default:
  901. return -EINVAL;
  902. }
  903. }
  904. /**
  905. * zpa2326_sample_oneshot() - Perform a complete one shot sampling cycle.
  906. * @indio_dev: The IIO device associated with the sampling hardware.
  907. * @type: Type of measurement / channel to fetch from.
  908. * @value: Sample output.
  909. *
  910. * Return: Zero when successful, a negative error code otherwise.
  911. */
  912. static int zpa2326_sample_oneshot(struct iio_dev *indio_dev,
  913. enum iio_chan_type type,
  914. int *value)
  915. {
  916. int ret;
  917. struct zpa2326_private *priv;
  918. ret = iio_device_claim_direct_mode(indio_dev);
  919. if (ret)
  920. return ret;
  921. ret = zpa2326_resume(indio_dev);
  922. if (ret < 0)
  923. goto release;
  924. priv = iio_priv(indio_dev);
  925. if (ret > 0) {
  926. /*
  927. * We were already power supplied. Just clear hardware FIFO to
  928. * get rid of samples acquired during previous rounds (if any).
  929. * Sampling operation always generates both temperature and
  930. * pressure samples. The latter are always enqueued into
  931. * hardware FIFO. This may lead to situations were pressure
  932. * samples still sit into FIFO when previous cycle(s) fetched
  933. * temperature data only.
  934. * Hence, we need to clear hardware FIFO content to prevent from
  935. * getting outdated values at the end of current cycle.
  936. */
  937. if (type == IIO_PRESSURE) {
  938. ret = zpa2326_clear_fifo(indio_dev, 0);
  939. if (ret)
  940. goto suspend;
  941. }
  942. } else {
  943. /*
  944. * We have just been power supplied, i.e. device is in default
  945. * "out of reset" state, meaning we need to reconfigure it
  946. * entirely.
  947. */
  948. ret = zpa2326_config_oneshot(indio_dev, priv->irq);
  949. if (ret)
  950. goto suspend;
  951. }
  952. /* Start a sampling cycle in oneshot mode. */
  953. ret = zpa2326_start_oneshot(indio_dev);
  954. if (ret)
  955. goto suspend;
  956. /* Wait for sampling cycle to complete. */
  957. if (priv->irq > 0)
  958. ret = zpa2326_wait_oneshot_completion(indio_dev, priv);
  959. else
  960. ret = zpa2326_poll_oneshot_completion(indio_dev);
  961. if (ret)
  962. goto suspend;
  963. /* Retrieve raw sample value and convert it to CPU endianness. */
  964. ret = zpa2326_fetch_raw_sample(indio_dev, type, value);
  965. suspend:
  966. zpa2326_suspend(indio_dev);
  967. release:
  968. iio_device_release_direct_mode(indio_dev);
  969. return ret;
  970. }
  971. /**
  972. * zpa2326_trigger_handler() - Perform an IIO buffered sampling round in one
  973. * shot mode.
  974. * @irq: The software interrupt assigned to @data
  975. * @data: The IIO poll function dispatched by external trigger our device is
  976. * attached to.
  977. *
  978. * Bottom-half handler called by the IIO trigger to which our device is
  979. * currently attached. Allows us to synchronize this device buffered sampling
  980. * either with external events (such as timer expiration, external device sample
  981. * ready, etc...) or with its own interrupt (internal hardware trigger).
  982. *
  983. * When using an external trigger, basically run the same sequence of operations
  984. * as for zpa2326_sample_oneshot() with the following hereafter. Hardware FIFO
  985. * is not cleared since already done at buffering enable time and samples
  986. * dequeueing always retrieves the most recent value.
  987. *
  988. * Otherwise, when internal hardware trigger has dispatched us, just fetch data
  989. * from hardware FIFO.
  990. *
  991. * Fetched data will pushed unprocessed to IIO buffer since samples conversion
  992. * is delegated to userspace in buffered mode (endianness, etc...).
  993. *
  994. * Return:
  995. * %IRQ_NONE - no consistent interrupt happened ;
  996. * %IRQ_HANDLED - there was new samples available.
  997. */
  998. static irqreturn_t zpa2326_trigger_handler(int irq, void *data)
  999. {
  1000. struct iio_dev *indio_dev = ((struct iio_poll_func *)
  1001. data)->indio_dev;
  1002. struct zpa2326_private *priv = iio_priv(indio_dev);
  1003. bool cont;
  1004. /*
  1005. * We have been dispatched, meaning we are in triggered buffer mode.
  1006. * Using our own internal trigger implies we are currently in continuous
  1007. * hardware sampling mode.
  1008. */
  1009. cont = iio_trigger_using_own(indio_dev);
  1010. if (!cont) {
  1011. /* On demand sampling : start a one shot cycle. */
  1012. if (zpa2326_start_oneshot(indio_dev))
  1013. goto out;
  1014. /* Wait for sampling cycle to complete. */
  1015. if (priv->irq <= 0) {
  1016. /* No interrupt available: poll for completion. */
  1017. if (zpa2326_poll_oneshot_completion(indio_dev))
  1018. goto out;
  1019. /* Only timestamp sample once it is ready. */
  1020. priv->timestamp = iio_get_time_ns(indio_dev);
  1021. } else {
  1022. /* Interrupt handlers will timestamp for us. */
  1023. if (zpa2326_wait_oneshot_completion(indio_dev, priv))
  1024. goto out;
  1025. }
  1026. }
  1027. /* Enqueue to IIO buffer / userspace. */
  1028. zpa2326_fill_sample_buffer(indio_dev, priv);
  1029. out:
  1030. if (!cont)
  1031. /* Don't switch to low power if sampling continuously. */
  1032. zpa2326_sleep(indio_dev);
  1033. /* Inform attached trigger we are done. */
  1034. iio_trigger_notify_done(indio_dev->trig);
  1035. return IRQ_HANDLED;
  1036. }
  1037. /**
  1038. * zpa2326_preenable_buffer() - Prepare device for configuring triggered
  1039. * sampling
  1040. * modes.
  1041. * @indio_dev: The IIO device associated with the sampling hardware.
  1042. *
  1043. * Basically power up device.
  1044. * Called with IIO device's lock held.
  1045. *
  1046. * Return: Zero when successful, a negative error code otherwise.
  1047. */
  1048. static int zpa2326_preenable_buffer(struct iio_dev *indio_dev)
  1049. {
  1050. int ret = zpa2326_resume(indio_dev);
  1051. if (ret < 0)
  1052. return ret;
  1053. /* Tell zpa2326_postenable_buffer() if we have just been powered on. */
  1054. ((struct zpa2326_private *)
  1055. iio_priv(indio_dev))->waken = iio_priv(indio_dev);
  1056. return 0;
  1057. }
  1058. /**
  1059. * zpa2326_postenable_buffer() - Configure device for triggered sampling.
  1060. * @indio_dev: The IIO device associated with the sampling hardware.
  1061. *
  1062. * Basically setup one-shot mode if plugging external trigger.
  1063. * Otherwise, let internal trigger configure continuous sampling :
  1064. * see zpa2326_set_trigger_state().
  1065. *
  1066. * If an error is returned, IIO layer will call our postdisable hook for us,
  1067. * i.e. no need to explicitly power device off here.
  1068. * Called with IIO device's lock held.
  1069. *
  1070. * Called with IIO device's lock held.
  1071. *
  1072. * Return: Zero when successful, a negative error code otherwise.
  1073. */
  1074. static int zpa2326_postenable_buffer(struct iio_dev *indio_dev)
  1075. {
  1076. const struct zpa2326_private *priv = iio_priv(indio_dev);
  1077. int err;
  1078. if (!priv->waken) {
  1079. /*
  1080. * We were already power supplied. Just clear hardware FIFO to
  1081. * get rid of samples acquired during previous rounds (if any).
  1082. */
  1083. err = zpa2326_clear_fifo(indio_dev, 0);
  1084. if (err)
  1085. goto err;
  1086. }
  1087. if (!iio_trigger_using_own(indio_dev) && priv->waken) {
  1088. /*
  1089. * We are using an external trigger and we have just been
  1090. * powered up: reconfigure one-shot mode.
  1091. */
  1092. err = zpa2326_config_oneshot(indio_dev, priv->irq);
  1093. if (err)
  1094. goto err;
  1095. }
  1096. /* Plug our own trigger event handler. */
  1097. err = iio_triggered_buffer_postenable(indio_dev);
  1098. if (err)
  1099. goto err;
  1100. return 0;
  1101. err:
  1102. zpa2326_err(indio_dev, "failed to enable buffering (%d)", err);
  1103. return err;
  1104. }
  1105. static int zpa2326_postdisable_buffer(struct iio_dev *indio_dev)
  1106. {
  1107. zpa2326_suspend(indio_dev);
  1108. return 0;
  1109. }
  1110. static const struct iio_buffer_setup_ops zpa2326_buffer_setup_ops = {
  1111. .preenable = zpa2326_preenable_buffer,
  1112. .postenable = zpa2326_postenable_buffer,
  1113. .predisable = iio_triggered_buffer_predisable,
  1114. .postdisable = zpa2326_postdisable_buffer
  1115. };
  1116. /**
  1117. * zpa2326_set_trigger_state() - Start / stop continuous sampling.
  1118. * @trig: The trigger being attached to IIO device associated with the sampling
  1119. * hardware.
  1120. * @state: Tell whether to start (true) or stop (false)
  1121. *
  1122. * Basically enable / disable hardware continuous sampling mode.
  1123. *
  1124. * Called with IIO device's lock held at postenable() or predisable() time.
  1125. *
  1126. * Return: Zero when successful, a negative error code otherwise.
  1127. */
  1128. static int zpa2326_set_trigger_state(struct iio_trigger *trig, bool state)
  1129. {
  1130. const struct iio_dev *indio_dev = dev_get_drvdata(
  1131. trig->dev.parent);
  1132. const struct zpa2326_private *priv = iio_priv(indio_dev);
  1133. int err;
  1134. if (!state) {
  1135. /*
  1136. * Switch trigger off : in case of failure, interrupt is left
  1137. * disabled in order to prevent handler from accessing released
  1138. * resources.
  1139. */
  1140. unsigned int val;
  1141. /*
  1142. * As device is working in continuous mode, handlers may be
  1143. * accessing resources we are currently freeing...
  1144. * Prevent this by disabling interrupt handlers and ensure
  1145. * the device will generate no more interrupts unless explicitly
  1146. * required to, i.e. by restoring back to default one shot mode.
  1147. */
  1148. disable_irq(priv->irq);
  1149. /*
  1150. * Disable continuous sampling mode to restore settings for
  1151. * one shot / direct sampling operations.
  1152. */
  1153. err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
  1154. zpa2326_highest_frequency()->odr);
  1155. if (err)
  1156. return err;
  1157. /*
  1158. * Now that device won't generate interrupts on its own,
  1159. * acknowledge any currently active interrupts (may happen on
  1160. * rare occasions while stopping continuous mode).
  1161. */
  1162. err = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
  1163. if (err < 0)
  1164. return err;
  1165. /*
  1166. * Re-enable interrupts only if we can guarantee the device will
  1167. * generate no more interrupts to prevent handlers from
  1168. * accessing released resources.
  1169. */
  1170. enable_irq(priv->irq);
  1171. zpa2326_dbg(indio_dev, "continuous mode stopped");
  1172. } else {
  1173. /*
  1174. * Switch trigger on : start continuous sampling at required
  1175. * frequency.
  1176. */
  1177. if (priv->waken) {
  1178. /* Enable interrupt if getting out of reset. */
  1179. err = regmap_write(priv->regmap, ZPA2326_CTRL_REG1_REG,
  1180. (u8)
  1181. ~ZPA2326_CTRL_REG1_MASK_DATA_READY);
  1182. if (err)
  1183. return err;
  1184. }
  1185. /* Enable continuous sampling at specified frequency. */
  1186. err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
  1187. ZPA2326_CTRL_REG3_ENABLE_MEAS |
  1188. priv->frequency->odr);
  1189. if (err)
  1190. return err;
  1191. zpa2326_dbg(indio_dev, "continuous mode setup @%dHz",
  1192. priv->frequency->hz);
  1193. }
  1194. return 0;
  1195. }
  1196. static const struct iio_trigger_ops zpa2326_trigger_ops = {
  1197. .set_trigger_state = zpa2326_set_trigger_state,
  1198. };
  1199. /**
  1200. * zpa2326_init_trigger() - Create an interrupt driven / hardware trigger
  1201. * allowing to notify external devices a new sample is
  1202. * ready.
  1203. * @parent: Hardware sampling device @indio_dev is a child of.
  1204. * @indio_dev: The IIO device associated with the sampling hardware.
  1205. * @private: Internal private state related to @indio_dev.
  1206. * @irq: Optional interrupt line the hardware uses to notify new data
  1207. * samples are ready. Negative or zero values indicate no interrupts
  1208. * are available, meaning polling is required.
  1209. *
  1210. * Only relevant when DT declares a valid interrupt line.
  1211. *
  1212. * Return: Zero when successful, a negative error code otherwise.
  1213. */
  1214. static int zpa2326_init_managed_trigger(struct device *parent,
  1215. struct iio_dev *indio_dev,
  1216. struct zpa2326_private *private,
  1217. int irq)
  1218. {
  1219. struct iio_trigger *trigger;
  1220. int ret;
  1221. if (irq <= 0)
  1222. return 0;
  1223. trigger = devm_iio_trigger_alloc(parent, "%s-dev%d",
  1224. indio_dev->name, indio_dev->id);
  1225. if (!trigger)
  1226. return -ENOMEM;
  1227. /* Basic setup. */
  1228. trigger->dev.parent = parent;
  1229. trigger->ops = &zpa2326_trigger_ops;
  1230. private->trigger = trigger;
  1231. /* Register to triggers space. */
  1232. ret = devm_iio_trigger_register(parent, trigger);
  1233. if (ret)
  1234. dev_err(parent, "failed to register hardware trigger (%d)",
  1235. ret);
  1236. return ret;
  1237. }
  1238. static int zpa2326_get_frequency(const struct iio_dev *indio_dev)
  1239. {
  1240. return ((struct zpa2326_private *)iio_priv(indio_dev))->frequency->hz;
  1241. }
  1242. static int zpa2326_set_frequency(struct iio_dev *indio_dev, int hz)
  1243. {
  1244. struct zpa2326_private *priv = iio_priv(indio_dev);
  1245. int freq;
  1246. int err;
  1247. /* Check if requested frequency is supported. */
  1248. for (freq = 0; freq < ARRAY_SIZE(zpa2326_sampling_frequencies); freq++)
  1249. if (zpa2326_sampling_frequencies[freq].hz == hz)
  1250. break;
  1251. if (freq == ARRAY_SIZE(zpa2326_sampling_frequencies))
  1252. return -EINVAL;
  1253. /* Don't allow changing frequency if buffered sampling is ongoing. */
  1254. err = iio_device_claim_direct_mode(indio_dev);
  1255. if (err)
  1256. return err;
  1257. priv->frequency = &zpa2326_sampling_frequencies[freq];
  1258. iio_device_release_direct_mode(indio_dev);
  1259. return 0;
  1260. }
  1261. /* Expose supported hardware sampling frequencies (Hz) through sysfs. */
  1262. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1 5 11 23");
  1263. static struct attribute *zpa2326_attributes[] = {
  1264. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  1265. NULL
  1266. };
  1267. static const struct attribute_group zpa2326_attribute_group = {
  1268. .attrs = zpa2326_attributes,
  1269. };
  1270. static int zpa2326_read_raw(struct iio_dev *indio_dev,
  1271. struct iio_chan_spec const *chan,
  1272. int *val,
  1273. int *val2,
  1274. long mask)
  1275. {
  1276. switch (mask) {
  1277. case IIO_CHAN_INFO_RAW:
  1278. return zpa2326_sample_oneshot(indio_dev, chan->type, val);
  1279. case IIO_CHAN_INFO_SCALE:
  1280. switch (chan->type) {
  1281. case IIO_PRESSURE:
  1282. /*
  1283. * Pressure resolution is 1/64 Pascal. Scale to kPascal
  1284. * as required by IIO ABI.
  1285. */
  1286. *val = 1;
  1287. *val2 = 64000;
  1288. return IIO_VAL_FRACTIONAL;
  1289. case IIO_TEMP:
  1290. /*
  1291. * Temperature follows the equation:
  1292. * Temp[degC] = Tempcode * 0.00649 - 176.83
  1293. * where:
  1294. * Tempcode is composed the raw sampled 16 bits.
  1295. *
  1296. * Hence, to produce a temperature in milli-degrees
  1297. * Celsius according to IIO ABI, we need to apply the
  1298. * following equation to raw samples:
  1299. * Temp[milli degC] = (Tempcode + Offset) * Scale
  1300. * where:
  1301. * Offset = -176.83 / 0.00649
  1302. * Scale = 0.00649 * 1000
  1303. */
  1304. *val = 6;
  1305. *val2 = 490000;
  1306. return IIO_VAL_INT_PLUS_MICRO;
  1307. default:
  1308. return -EINVAL;
  1309. }
  1310. case IIO_CHAN_INFO_OFFSET:
  1311. switch (chan->type) {
  1312. case IIO_TEMP:
  1313. *val = -17683000;
  1314. *val2 = 649;
  1315. return IIO_VAL_FRACTIONAL;
  1316. default:
  1317. return -EINVAL;
  1318. }
  1319. case IIO_CHAN_INFO_SAMP_FREQ:
  1320. *val = zpa2326_get_frequency(indio_dev);
  1321. return IIO_VAL_INT;
  1322. default:
  1323. return -EINVAL;
  1324. }
  1325. }
  1326. static int zpa2326_write_raw(struct iio_dev *indio_dev,
  1327. const struct iio_chan_spec *chan,
  1328. int val,
  1329. int val2,
  1330. long mask)
  1331. {
  1332. if ((mask != IIO_CHAN_INFO_SAMP_FREQ) || val2)
  1333. return -EINVAL;
  1334. return zpa2326_set_frequency(indio_dev, val);
  1335. }
  1336. static const struct iio_chan_spec zpa2326_channels[] = {
  1337. [0] = {
  1338. .type = IIO_PRESSURE,
  1339. .scan_index = 0,
  1340. .scan_type = {
  1341. .sign = 'u',
  1342. .realbits = 24,
  1343. .storagebits = 32,
  1344. .endianness = IIO_LE,
  1345. },
  1346. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1347. BIT(IIO_CHAN_INFO_SCALE),
  1348. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  1349. },
  1350. [1] = {
  1351. .type = IIO_TEMP,
  1352. .scan_index = 1,
  1353. .scan_type = {
  1354. .sign = 's',
  1355. .realbits = 16,
  1356. .storagebits = 16,
  1357. .endianness = IIO_LE,
  1358. },
  1359. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  1360. BIT(IIO_CHAN_INFO_SCALE) |
  1361. BIT(IIO_CHAN_INFO_OFFSET),
  1362. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  1363. },
  1364. [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
  1365. };
  1366. static const struct iio_info zpa2326_info = {
  1367. .attrs = &zpa2326_attribute_group,
  1368. .read_raw = zpa2326_read_raw,
  1369. .write_raw = zpa2326_write_raw,
  1370. };
  1371. static struct iio_dev *zpa2326_create_managed_iiodev(struct device *device,
  1372. const char *name,
  1373. struct regmap *regmap)
  1374. {
  1375. struct iio_dev *indio_dev;
  1376. /* Allocate space to hold IIO device internal state. */
  1377. indio_dev = devm_iio_device_alloc(device,
  1378. sizeof(struct zpa2326_private));
  1379. if (!indio_dev)
  1380. return NULL;
  1381. /* Setup for userspace synchronous on demand sampling. */
  1382. indio_dev->modes = INDIO_DIRECT_MODE;
  1383. indio_dev->dev.parent = device;
  1384. indio_dev->channels = zpa2326_channels;
  1385. indio_dev->num_channels = ARRAY_SIZE(zpa2326_channels);
  1386. indio_dev->name = name;
  1387. indio_dev->info = &zpa2326_info;
  1388. return indio_dev;
  1389. }
  1390. int zpa2326_probe(struct device *parent,
  1391. const char *name,
  1392. int irq,
  1393. unsigned int hwid,
  1394. struct regmap *regmap)
  1395. {
  1396. struct iio_dev *indio_dev;
  1397. struct zpa2326_private *priv;
  1398. int err;
  1399. unsigned int id;
  1400. indio_dev = zpa2326_create_managed_iiodev(parent, name, regmap);
  1401. if (!indio_dev)
  1402. return -ENOMEM;
  1403. priv = iio_priv(indio_dev);
  1404. priv->vref = devm_regulator_get(parent, "vref");
  1405. if (IS_ERR(priv->vref))
  1406. return PTR_ERR(priv->vref);
  1407. priv->vdd = devm_regulator_get(parent, "vdd");
  1408. if (IS_ERR(priv->vdd))
  1409. return PTR_ERR(priv->vdd);
  1410. /* Set default hardware sampling frequency to highest rate supported. */
  1411. priv->frequency = zpa2326_highest_frequency();
  1412. /*
  1413. * Plug device's underlying bus abstraction : this MUST be set before
  1414. * registering interrupt handlers since an interrupt might happen if
  1415. * power up sequence is not properly applied.
  1416. */
  1417. priv->regmap = regmap;
  1418. err = devm_iio_triggered_buffer_setup(parent, indio_dev, NULL,
  1419. zpa2326_trigger_handler,
  1420. &zpa2326_buffer_setup_ops);
  1421. if (err)
  1422. return err;
  1423. err = zpa2326_init_managed_trigger(parent, indio_dev, priv, irq);
  1424. if (err)
  1425. return err;
  1426. err = zpa2326_init_managed_irq(parent, indio_dev, priv, irq);
  1427. if (err)
  1428. return err;
  1429. /* Power up to check device ID and perform initial hardware setup. */
  1430. err = zpa2326_power_on(indio_dev, priv);
  1431. if (err)
  1432. return err;
  1433. /* Read id register to check we are talking to the right slave. */
  1434. err = regmap_read(regmap, ZPA2326_DEVICE_ID_REG, &id);
  1435. if (err)
  1436. goto sleep;
  1437. if (id != hwid) {
  1438. dev_err(parent, "found device with unexpected id %02x", id);
  1439. err = -ENODEV;
  1440. goto sleep;
  1441. }
  1442. err = zpa2326_config_oneshot(indio_dev, irq);
  1443. if (err)
  1444. goto sleep;
  1445. /* Setup done : go sleeping. Device will be awaken upon user request. */
  1446. err = zpa2326_sleep(indio_dev);
  1447. if (err)
  1448. goto poweroff;
  1449. dev_set_drvdata(parent, indio_dev);
  1450. zpa2326_init_runtime(parent);
  1451. err = iio_device_register(indio_dev);
  1452. if (err) {
  1453. zpa2326_fini_runtime(parent);
  1454. goto poweroff;
  1455. }
  1456. return 0;
  1457. sleep:
  1458. /* Put to sleep just in case power regulators are "dummy" ones. */
  1459. zpa2326_sleep(indio_dev);
  1460. poweroff:
  1461. zpa2326_power_off(indio_dev, priv);
  1462. return err;
  1463. }
  1464. EXPORT_SYMBOL_GPL(zpa2326_probe);
  1465. void zpa2326_remove(const struct device *parent)
  1466. {
  1467. struct iio_dev *indio_dev = dev_get_drvdata(parent);
  1468. iio_device_unregister(indio_dev);
  1469. zpa2326_fini_runtime(indio_dev->dev.parent);
  1470. zpa2326_sleep(indio_dev);
  1471. zpa2326_power_off(indio_dev, iio_priv(indio_dev));
  1472. }
  1473. EXPORT_SYMBOL_GPL(zpa2326_remove);
  1474. MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
  1475. MODULE_DESCRIPTION("Core driver for Murata ZPA2326 pressure sensor");
  1476. MODULE_LICENSE("GPL v2");