zpa2326.c 48 KB

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