stm32-timer-trigger.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * Copyright (C) STMicroelectronics 2016
  3. *
  4. * Author: Benjamin Gaignard <benjamin.gaignard@st.com>
  5. *
  6. * License terms: GNU General Public License (GPL), version 2
  7. */
  8. #include <linux/iio/iio.h>
  9. #include <linux/iio/sysfs.h>
  10. #include <linux/iio/timer/stm32-timer-trigger.h>
  11. #include <linux/iio/trigger.h>
  12. #include <linux/mfd/stm32-timers.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of_device.h>
  16. #define MAX_TRIGGERS 7
  17. #define MAX_VALIDS 5
  18. /* List the triggers created by each timer */
  19. static const void *triggers_table[][MAX_TRIGGERS] = {
  20. { TIM1_TRGO, TIM1_TRGO2, TIM1_CH1, TIM1_CH2, TIM1_CH3, TIM1_CH4,},
  21. { TIM2_TRGO, TIM2_CH1, TIM2_CH2, TIM2_CH3, TIM2_CH4,},
  22. { TIM3_TRGO, TIM3_CH1, TIM3_CH2, TIM3_CH3, TIM3_CH4,},
  23. { TIM4_TRGO, TIM4_CH1, TIM4_CH2, TIM4_CH3, TIM4_CH4,},
  24. { TIM5_TRGO, TIM5_CH1, TIM5_CH2, TIM5_CH3, TIM5_CH4,},
  25. { TIM6_TRGO,},
  26. { TIM7_TRGO,},
  27. { TIM8_TRGO, TIM8_TRGO2, TIM8_CH1, TIM8_CH2, TIM8_CH3, TIM8_CH4,},
  28. { TIM9_TRGO, TIM9_CH1, TIM9_CH2,},
  29. { }, /* timer 10 */
  30. { }, /* timer 11 */
  31. { TIM12_TRGO, TIM12_CH1, TIM12_CH2,},
  32. { }, /* timer 13 */
  33. { }, /* timer 14 */
  34. { TIM15_TRGO,},
  35. };
  36. /* List the triggers accepted by each timer */
  37. static const void *valids_table[][MAX_VALIDS] = {
  38. { TIM5_TRGO, TIM2_TRGO, TIM3_TRGO, TIM4_TRGO,},
  39. { TIM1_TRGO, TIM8_TRGO, TIM3_TRGO, TIM4_TRGO,},
  40. { TIM1_TRGO, TIM2_TRGO, TIM5_TRGO, TIM4_TRGO,},
  41. { TIM1_TRGO, TIM2_TRGO, TIM3_TRGO, TIM8_TRGO,},
  42. { TIM2_TRGO, TIM3_TRGO, TIM4_TRGO, TIM8_TRGO,},
  43. { }, /* timer 6 */
  44. { }, /* timer 7 */
  45. { TIM1_TRGO, TIM2_TRGO, TIM4_TRGO, TIM5_TRGO,},
  46. { TIM2_TRGO, TIM3_TRGO,},
  47. { }, /* timer 10 */
  48. { }, /* timer 11 */
  49. { TIM4_TRGO, TIM5_TRGO,},
  50. };
  51. static const void *stm32h7_valids_table[][MAX_VALIDS] = {
  52. { TIM15_TRGO, TIM2_TRGO, TIM3_TRGO, TIM4_TRGO,},
  53. { TIM1_TRGO, TIM8_TRGO, TIM3_TRGO, TIM4_TRGO,},
  54. { TIM1_TRGO, TIM2_TRGO, TIM15_TRGO, TIM4_TRGO,},
  55. { TIM1_TRGO, TIM2_TRGO, TIM3_TRGO, TIM8_TRGO,},
  56. { TIM1_TRGO, TIM8_TRGO, TIM3_TRGO, TIM4_TRGO,},
  57. { }, /* timer 6 */
  58. { }, /* timer 7 */
  59. { TIM1_TRGO, TIM2_TRGO, TIM4_TRGO, TIM5_TRGO,},
  60. { }, /* timer 9 */
  61. { }, /* timer 10 */
  62. { }, /* timer 11 */
  63. { TIM4_TRGO, TIM5_TRGO,},
  64. { }, /* timer 13 */
  65. { }, /* timer 14 */
  66. { TIM1_TRGO, TIM3_TRGO,},
  67. };
  68. struct stm32_timer_trigger {
  69. struct device *dev;
  70. struct regmap *regmap;
  71. struct clk *clk;
  72. u32 max_arr;
  73. const void *triggers;
  74. const void *valids;
  75. bool has_trgo2;
  76. };
  77. struct stm32_timer_trigger_cfg {
  78. const void *(*valids_table)[MAX_VALIDS];
  79. const unsigned int num_valids_table;
  80. };
  81. static bool stm32_timer_is_trgo2_name(const char *name)
  82. {
  83. return !!strstr(name, "trgo2");
  84. }
  85. static int stm32_timer_start(struct stm32_timer_trigger *priv,
  86. struct iio_trigger *trig,
  87. unsigned int frequency)
  88. {
  89. unsigned long long prd, div;
  90. int prescaler = 0;
  91. u32 ccer, cr1;
  92. /* Period and prescaler values depends of clock rate */
  93. div = (unsigned long long)clk_get_rate(priv->clk);
  94. do_div(div, frequency);
  95. prd = div;
  96. /*
  97. * Increase prescaler value until we get a result that fit
  98. * with auto reload register maximum value.
  99. */
  100. while (div > priv->max_arr) {
  101. prescaler++;
  102. div = prd;
  103. do_div(div, (prescaler + 1));
  104. }
  105. prd = div;
  106. if (prescaler > MAX_TIM_PSC) {
  107. dev_err(priv->dev, "prescaler exceeds the maximum value\n");
  108. return -EINVAL;
  109. }
  110. /* Check if nobody else use the timer */
  111. regmap_read(priv->regmap, TIM_CCER, &ccer);
  112. if (ccer & TIM_CCER_CCXE)
  113. return -EBUSY;
  114. regmap_read(priv->regmap, TIM_CR1, &cr1);
  115. if (!(cr1 & TIM_CR1_CEN))
  116. clk_enable(priv->clk);
  117. regmap_write(priv->regmap, TIM_PSC, prescaler);
  118. regmap_write(priv->regmap, TIM_ARR, prd - 1);
  119. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, TIM_CR1_ARPE);
  120. /* Force master mode to update mode */
  121. if (stm32_timer_is_trgo2_name(trig->name))
  122. regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS2,
  123. 0x2 << TIM_CR2_MMS2_SHIFT);
  124. else
  125. regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS,
  126. 0x2 << TIM_CR2_MMS_SHIFT);
  127. /* Make sure that registers are updated */
  128. regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
  129. /* Enable controller */
  130. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, TIM_CR1_CEN);
  131. return 0;
  132. }
  133. static void stm32_timer_stop(struct stm32_timer_trigger *priv)
  134. {
  135. u32 ccer, cr1;
  136. regmap_read(priv->regmap, TIM_CCER, &ccer);
  137. if (ccer & TIM_CCER_CCXE)
  138. return;
  139. regmap_read(priv->regmap, TIM_CR1, &cr1);
  140. if (cr1 & TIM_CR1_CEN)
  141. clk_disable(priv->clk);
  142. /* Stop timer */
  143. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_CEN, 0);
  144. regmap_write(priv->regmap, TIM_PSC, 0);
  145. regmap_write(priv->regmap, TIM_ARR, 0);
  146. /* Make sure that registers are updated */
  147. regmap_update_bits(priv->regmap, TIM_EGR, TIM_EGR_UG, TIM_EGR_UG);
  148. }
  149. static ssize_t stm32_tt_store_frequency(struct device *dev,
  150. struct device_attribute *attr,
  151. const char *buf, size_t len)
  152. {
  153. struct iio_trigger *trig = to_iio_trigger(dev);
  154. struct stm32_timer_trigger *priv = iio_trigger_get_drvdata(trig);
  155. unsigned int freq;
  156. int ret;
  157. ret = kstrtouint(buf, 10, &freq);
  158. if (ret)
  159. return ret;
  160. if (freq == 0) {
  161. stm32_timer_stop(priv);
  162. } else {
  163. ret = stm32_timer_start(priv, trig, freq);
  164. if (ret)
  165. return ret;
  166. }
  167. return len;
  168. }
  169. static ssize_t stm32_tt_read_frequency(struct device *dev,
  170. struct device_attribute *attr, char *buf)
  171. {
  172. struct iio_trigger *trig = to_iio_trigger(dev);
  173. struct stm32_timer_trigger *priv = iio_trigger_get_drvdata(trig);
  174. u32 psc, arr, cr1;
  175. unsigned long long freq = 0;
  176. regmap_read(priv->regmap, TIM_CR1, &cr1);
  177. regmap_read(priv->regmap, TIM_PSC, &psc);
  178. regmap_read(priv->regmap, TIM_ARR, &arr);
  179. if (cr1 & TIM_CR1_CEN) {
  180. freq = (unsigned long long)clk_get_rate(priv->clk);
  181. do_div(freq, psc + 1);
  182. do_div(freq, arr + 1);
  183. }
  184. return sprintf(buf, "%d\n", (unsigned int)freq);
  185. }
  186. static IIO_DEV_ATTR_SAMP_FREQ(0660,
  187. stm32_tt_read_frequency,
  188. stm32_tt_store_frequency);
  189. #define MASTER_MODE_MAX 7
  190. #define MASTER_MODE2_MAX 15
  191. static char *master_mode_table[] = {
  192. "reset",
  193. "enable",
  194. "update",
  195. "compare_pulse",
  196. "OC1REF",
  197. "OC2REF",
  198. "OC3REF",
  199. "OC4REF",
  200. /* Master mode selection 2 only */
  201. "OC5REF",
  202. "OC6REF",
  203. "compare_pulse_OC4REF",
  204. "compare_pulse_OC6REF",
  205. "compare_pulse_OC4REF_r_or_OC6REF_r",
  206. "compare_pulse_OC4REF_r_or_OC6REF_f",
  207. "compare_pulse_OC5REF_r_or_OC6REF_r",
  208. "compare_pulse_OC5REF_r_or_OC6REF_f",
  209. };
  210. static ssize_t stm32_tt_show_master_mode(struct device *dev,
  211. struct device_attribute *attr,
  212. char *buf)
  213. {
  214. struct stm32_timer_trigger *priv = dev_get_drvdata(dev);
  215. struct iio_trigger *trig = to_iio_trigger(dev);
  216. u32 cr2;
  217. regmap_read(priv->regmap, TIM_CR2, &cr2);
  218. if (stm32_timer_is_trgo2_name(trig->name))
  219. cr2 = (cr2 & TIM_CR2_MMS2) >> TIM_CR2_MMS2_SHIFT;
  220. else
  221. cr2 = (cr2 & TIM_CR2_MMS) >> TIM_CR2_MMS_SHIFT;
  222. return snprintf(buf, PAGE_SIZE, "%s\n", master_mode_table[cr2]);
  223. }
  224. static ssize_t stm32_tt_store_master_mode(struct device *dev,
  225. struct device_attribute *attr,
  226. const char *buf, size_t len)
  227. {
  228. struct stm32_timer_trigger *priv = dev_get_drvdata(dev);
  229. struct iio_trigger *trig = to_iio_trigger(dev);
  230. u32 mask, shift, master_mode_max;
  231. int i;
  232. if (stm32_timer_is_trgo2_name(trig->name)) {
  233. mask = TIM_CR2_MMS2;
  234. shift = TIM_CR2_MMS2_SHIFT;
  235. master_mode_max = MASTER_MODE2_MAX;
  236. } else {
  237. mask = TIM_CR2_MMS;
  238. shift = TIM_CR2_MMS_SHIFT;
  239. master_mode_max = MASTER_MODE_MAX;
  240. }
  241. for (i = 0; i <= master_mode_max; i++) {
  242. if (!strncmp(master_mode_table[i], buf,
  243. strlen(master_mode_table[i]))) {
  244. regmap_update_bits(priv->regmap, TIM_CR2, mask,
  245. i << shift);
  246. /* Make sure that registers are updated */
  247. regmap_update_bits(priv->regmap, TIM_EGR,
  248. TIM_EGR_UG, TIM_EGR_UG);
  249. return len;
  250. }
  251. }
  252. return -EINVAL;
  253. }
  254. static ssize_t stm32_tt_show_master_mode_avail(struct device *dev,
  255. struct device_attribute *attr,
  256. char *buf)
  257. {
  258. struct iio_trigger *trig = to_iio_trigger(dev);
  259. unsigned int i, master_mode_max;
  260. size_t len = 0;
  261. if (stm32_timer_is_trgo2_name(trig->name))
  262. master_mode_max = MASTER_MODE2_MAX;
  263. else
  264. master_mode_max = MASTER_MODE_MAX;
  265. for (i = 0; i <= master_mode_max; i++)
  266. len += scnprintf(buf + len, PAGE_SIZE - len,
  267. "%s ", master_mode_table[i]);
  268. /* replace trailing space by newline */
  269. buf[len - 1] = '\n';
  270. return len;
  271. }
  272. static IIO_DEVICE_ATTR(master_mode_available, 0444,
  273. stm32_tt_show_master_mode_avail, NULL, 0);
  274. static IIO_DEVICE_ATTR(master_mode, 0660,
  275. stm32_tt_show_master_mode,
  276. stm32_tt_store_master_mode,
  277. 0);
  278. static struct attribute *stm32_trigger_attrs[] = {
  279. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  280. &iio_dev_attr_master_mode.dev_attr.attr,
  281. &iio_dev_attr_master_mode_available.dev_attr.attr,
  282. NULL,
  283. };
  284. static const struct attribute_group stm32_trigger_attr_group = {
  285. .attrs = stm32_trigger_attrs,
  286. };
  287. static const struct attribute_group *stm32_trigger_attr_groups[] = {
  288. &stm32_trigger_attr_group,
  289. NULL,
  290. };
  291. static const struct iio_trigger_ops timer_trigger_ops = {
  292. .owner = THIS_MODULE,
  293. };
  294. static int stm32_setup_iio_triggers(struct stm32_timer_trigger *priv)
  295. {
  296. int ret;
  297. const char * const *cur = priv->triggers;
  298. while (cur && *cur) {
  299. struct iio_trigger *trig;
  300. bool cur_is_trgo2 = stm32_timer_is_trgo2_name(*cur);
  301. if (cur_is_trgo2 && !priv->has_trgo2) {
  302. cur++;
  303. continue;
  304. }
  305. trig = devm_iio_trigger_alloc(priv->dev, "%s", *cur);
  306. if (!trig)
  307. return -ENOMEM;
  308. trig->dev.parent = priv->dev->parent;
  309. trig->ops = &timer_trigger_ops;
  310. /*
  311. * sampling frequency and master mode attributes
  312. * should only be available on trgo trigger which
  313. * is always the first in the list.
  314. */
  315. if (cur == priv->triggers || cur_is_trgo2)
  316. trig->dev.groups = stm32_trigger_attr_groups;
  317. iio_trigger_set_drvdata(trig, priv);
  318. ret = devm_iio_trigger_register(priv->dev, trig);
  319. if (ret)
  320. return ret;
  321. cur++;
  322. }
  323. return 0;
  324. }
  325. static int stm32_counter_read_raw(struct iio_dev *indio_dev,
  326. struct iio_chan_spec const *chan,
  327. int *val, int *val2, long mask)
  328. {
  329. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  330. switch (mask) {
  331. case IIO_CHAN_INFO_RAW:
  332. {
  333. u32 cnt;
  334. regmap_read(priv->regmap, TIM_CNT, &cnt);
  335. *val = cnt;
  336. return IIO_VAL_INT;
  337. }
  338. case IIO_CHAN_INFO_SCALE:
  339. {
  340. u32 smcr;
  341. regmap_read(priv->regmap, TIM_SMCR, &smcr);
  342. smcr &= TIM_SMCR_SMS;
  343. *val = 1;
  344. *val2 = 0;
  345. /* in quadrature case scale = 0.25 */
  346. if (smcr == 3)
  347. *val2 = 2;
  348. return IIO_VAL_FRACTIONAL_LOG2;
  349. }
  350. }
  351. return -EINVAL;
  352. }
  353. static int stm32_counter_write_raw(struct iio_dev *indio_dev,
  354. struct iio_chan_spec const *chan,
  355. int val, int val2, long mask)
  356. {
  357. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  358. switch (mask) {
  359. case IIO_CHAN_INFO_RAW:
  360. regmap_write(priv->regmap, TIM_CNT, val);
  361. return IIO_VAL_INT;
  362. case IIO_CHAN_INFO_SCALE:
  363. /* fixed scale */
  364. return -EINVAL;
  365. }
  366. return -EINVAL;
  367. }
  368. static int stm32_counter_validate_trigger(struct iio_dev *indio_dev,
  369. struct iio_trigger *trig)
  370. {
  371. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  372. const char * const *cur = priv->valids;
  373. unsigned int i = 0;
  374. if (!is_stm32_timer_trigger(trig))
  375. return -EINVAL;
  376. while (cur && *cur) {
  377. if (!strncmp(trig->name, *cur, strlen(trig->name))) {
  378. regmap_update_bits(priv->regmap,
  379. TIM_SMCR, TIM_SMCR_TS,
  380. i << TIM_SMCR_TS_SHIFT);
  381. return 0;
  382. }
  383. cur++;
  384. i++;
  385. }
  386. return -EINVAL;
  387. }
  388. static const struct iio_info stm32_trigger_info = {
  389. .driver_module = THIS_MODULE,
  390. .validate_trigger = stm32_counter_validate_trigger,
  391. .read_raw = stm32_counter_read_raw,
  392. .write_raw = stm32_counter_write_raw
  393. };
  394. static const char *const stm32_trigger_modes[] = {
  395. "trigger",
  396. };
  397. static int stm32_set_trigger_mode(struct iio_dev *indio_dev,
  398. const struct iio_chan_spec *chan,
  399. unsigned int mode)
  400. {
  401. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  402. regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, TIM_SMCR_SMS);
  403. return 0;
  404. }
  405. static int stm32_get_trigger_mode(struct iio_dev *indio_dev,
  406. const struct iio_chan_spec *chan)
  407. {
  408. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  409. u32 smcr;
  410. regmap_read(priv->regmap, TIM_SMCR, &smcr);
  411. return smcr == TIM_SMCR_SMS ? 0 : -EINVAL;
  412. }
  413. static const struct iio_enum stm32_trigger_mode_enum = {
  414. .items = stm32_trigger_modes,
  415. .num_items = ARRAY_SIZE(stm32_trigger_modes),
  416. .set = stm32_set_trigger_mode,
  417. .get = stm32_get_trigger_mode
  418. };
  419. static const char *const stm32_enable_modes[] = {
  420. "always",
  421. "gated",
  422. "triggered",
  423. };
  424. static int stm32_enable_mode2sms(int mode)
  425. {
  426. switch (mode) {
  427. case 0:
  428. return 0;
  429. case 1:
  430. return 5;
  431. case 2:
  432. return 6;
  433. }
  434. return -EINVAL;
  435. }
  436. static int stm32_set_enable_mode(struct iio_dev *indio_dev,
  437. const struct iio_chan_spec *chan,
  438. unsigned int mode)
  439. {
  440. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  441. int sms = stm32_enable_mode2sms(mode);
  442. if (sms < 0)
  443. return sms;
  444. regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
  445. return 0;
  446. }
  447. static int stm32_sms2enable_mode(int mode)
  448. {
  449. switch (mode) {
  450. case 0:
  451. return 0;
  452. case 5:
  453. return 1;
  454. case 6:
  455. return 2;
  456. }
  457. return -EINVAL;
  458. }
  459. static int stm32_get_enable_mode(struct iio_dev *indio_dev,
  460. const struct iio_chan_spec *chan)
  461. {
  462. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  463. u32 smcr;
  464. regmap_read(priv->regmap, TIM_SMCR, &smcr);
  465. smcr &= TIM_SMCR_SMS;
  466. return stm32_sms2enable_mode(smcr);
  467. }
  468. static const struct iio_enum stm32_enable_mode_enum = {
  469. .items = stm32_enable_modes,
  470. .num_items = ARRAY_SIZE(stm32_enable_modes),
  471. .set = stm32_set_enable_mode,
  472. .get = stm32_get_enable_mode
  473. };
  474. static const char *const stm32_quadrature_modes[] = {
  475. "channel_A",
  476. "channel_B",
  477. "quadrature",
  478. };
  479. static int stm32_set_quadrature_mode(struct iio_dev *indio_dev,
  480. const struct iio_chan_spec *chan,
  481. unsigned int mode)
  482. {
  483. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  484. regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, mode + 1);
  485. return 0;
  486. }
  487. static int stm32_get_quadrature_mode(struct iio_dev *indio_dev,
  488. const struct iio_chan_spec *chan)
  489. {
  490. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  491. u32 smcr;
  492. regmap_read(priv->regmap, TIM_SMCR, &smcr);
  493. smcr &= TIM_SMCR_SMS;
  494. return smcr - 1;
  495. }
  496. static const struct iio_enum stm32_quadrature_mode_enum = {
  497. .items = stm32_quadrature_modes,
  498. .num_items = ARRAY_SIZE(stm32_quadrature_modes),
  499. .set = stm32_set_quadrature_mode,
  500. .get = stm32_get_quadrature_mode
  501. };
  502. static const char *const stm32_count_direction_states[] = {
  503. "up",
  504. "down"
  505. };
  506. static int stm32_set_count_direction(struct iio_dev *indio_dev,
  507. const struct iio_chan_spec *chan,
  508. unsigned int mode)
  509. {
  510. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  511. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_DIR, mode);
  512. return 0;
  513. }
  514. static int stm32_get_count_direction(struct iio_dev *indio_dev,
  515. const struct iio_chan_spec *chan)
  516. {
  517. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  518. u32 cr1;
  519. regmap_read(priv->regmap, TIM_CR1, &cr1);
  520. return (cr1 & TIM_CR1_DIR);
  521. }
  522. static const struct iio_enum stm32_count_direction_enum = {
  523. .items = stm32_count_direction_states,
  524. .num_items = ARRAY_SIZE(stm32_count_direction_states),
  525. .set = stm32_set_count_direction,
  526. .get = stm32_get_count_direction
  527. };
  528. static ssize_t stm32_count_get_preset(struct iio_dev *indio_dev,
  529. uintptr_t private,
  530. const struct iio_chan_spec *chan,
  531. char *buf)
  532. {
  533. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  534. u32 arr;
  535. regmap_read(priv->regmap, TIM_ARR, &arr);
  536. return snprintf(buf, PAGE_SIZE, "%u\n", arr);
  537. }
  538. static ssize_t stm32_count_set_preset(struct iio_dev *indio_dev,
  539. uintptr_t private,
  540. const struct iio_chan_spec *chan,
  541. const char *buf, size_t len)
  542. {
  543. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  544. unsigned int preset;
  545. int ret;
  546. ret = kstrtouint(buf, 0, &preset);
  547. if (ret)
  548. return ret;
  549. regmap_write(priv->regmap, TIM_ARR, preset);
  550. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, TIM_CR1_ARPE);
  551. return len;
  552. }
  553. static const struct iio_chan_spec_ext_info stm32_trigger_count_info[] = {
  554. {
  555. .name = "preset",
  556. .shared = IIO_SEPARATE,
  557. .read = stm32_count_get_preset,
  558. .write = stm32_count_set_preset
  559. },
  560. IIO_ENUM("count_direction", IIO_SEPARATE, &stm32_count_direction_enum),
  561. IIO_ENUM_AVAILABLE("count_direction", &stm32_count_direction_enum),
  562. IIO_ENUM("quadrature_mode", IIO_SEPARATE, &stm32_quadrature_mode_enum),
  563. IIO_ENUM_AVAILABLE("quadrature_mode", &stm32_quadrature_mode_enum),
  564. IIO_ENUM("enable_mode", IIO_SEPARATE, &stm32_enable_mode_enum),
  565. IIO_ENUM_AVAILABLE("enable_mode", &stm32_enable_mode_enum),
  566. IIO_ENUM("trigger_mode", IIO_SEPARATE, &stm32_trigger_mode_enum),
  567. IIO_ENUM_AVAILABLE("trigger_mode", &stm32_trigger_mode_enum),
  568. {}
  569. };
  570. static const struct iio_chan_spec stm32_trigger_channel = {
  571. .type = IIO_COUNT,
  572. .channel = 0,
  573. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  574. .ext_info = stm32_trigger_count_info,
  575. .indexed = 1
  576. };
  577. static struct stm32_timer_trigger *stm32_setup_counter_device(struct device *dev)
  578. {
  579. struct iio_dev *indio_dev;
  580. int ret;
  581. indio_dev = devm_iio_device_alloc(dev,
  582. sizeof(struct stm32_timer_trigger));
  583. if (!indio_dev)
  584. return NULL;
  585. indio_dev->name = dev_name(dev);
  586. indio_dev->dev.parent = dev;
  587. indio_dev->info = &stm32_trigger_info;
  588. indio_dev->modes = INDIO_HARDWARE_TRIGGERED;
  589. indio_dev->num_channels = 1;
  590. indio_dev->channels = &stm32_trigger_channel;
  591. indio_dev->dev.of_node = dev->of_node;
  592. ret = devm_iio_device_register(dev, indio_dev);
  593. if (ret)
  594. return NULL;
  595. return iio_priv(indio_dev);
  596. }
  597. /**
  598. * is_stm32_timer_trigger
  599. * @trig: trigger to be checked
  600. *
  601. * return true if the trigger is a valid stm32 iio timer trigger
  602. * either return false
  603. */
  604. bool is_stm32_timer_trigger(struct iio_trigger *trig)
  605. {
  606. return (trig->ops == &timer_trigger_ops);
  607. }
  608. EXPORT_SYMBOL(is_stm32_timer_trigger);
  609. static void stm32_timer_detect_trgo2(struct stm32_timer_trigger *priv)
  610. {
  611. u32 val;
  612. /*
  613. * Master mode selection 2 bits can only be written and read back when
  614. * timer supports it.
  615. */
  616. regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS2, TIM_CR2_MMS2);
  617. regmap_read(priv->regmap, TIM_CR2, &val);
  618. regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS2, 0);
  619. priv->has_trgo2 = !!val;
  620. }
  621. static int stm32_timer_trigger_probe(struct platform_device *pdev)
  622. {
  623. struct device *dev = &pdev->dev;
  624. struct stm32_timer_trigger *priv;
  625. struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
  626. const struct stm32_timer_trigger_cfg *cfg;
  627. unsigned int index;
  628. int ret;
  629. if (of_property_read_u32(dev->of_node, "reg", &index))
  630. return -EINVAL;
  631. cfg = (const struct stm32_timer_trigger_cfg *)
  632. of_match_device(dev->driver->of_match_table, dev)->data;
  633. if (index >= ARRAY_SIZE(triggers_table) ||
  634. index >= cfg->num_valids_table)
  635. return -EINVAL;
  636. /* Create an IIO device only if we have triggers to be validated */
  637. if (*cfg->valids_table[index])
  638. priv = stm32_setup_counter_device(dev);
  639. else
  640. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  641. if (!priv)
  642. return -ENOMEM;
  643. priv->dev = dev;
  644. priv->regmap = ddata->regmap;
  645. priv->clk = ddata->clk;
  646. priv->max_arr = ddata->max_arr;
  647. priv->triggers = triggers_table[index];
  648. priv->valids = cfg->valids_table[index];
  649. stm32_timer_detect_trgo2(priv);
  650. ret = stm32_setup_iio_triggers(priv);
  651. if (ret)
  652. return ret;
  653. platform_set_drvdata(pdev, priv);
  654. return 0;
  655. }
  656. static const struct stm32_timer_trigger_cfg stm32_timer_trg_cfg = {
  657. .valids_table = valids_table,
  658. .num_valids_table = ARRAY_SIZE(valids_table),
  659. };
  660. static const struct stm32_timer_trigger_cfg stm32h7_timer_trg_cfg = {
  661. .valids_table = stm32h7_valids_table,
  662. .num_valids_table = ARRAY_SIZE(stm32h7_valids_table),
  663. };
  664. static const struct of_device_id stm32_trig_of_match[] = {
  665. {
  666. .compatible = "st,stm32-timer-trigger",
  667. .data = (void *)&stm32_timer_trg_cfg,
  668. }, {
  669. .compatible = "st,stm32h7-timer-trigger",
  670. .data = (void *)&stm32h7_timer_trg_cfg,
  671. },
  672. { /* end node */ },
  673. };
  674. MODULE_DEVICE_TABLE(of, stm32_trig_of_match);
  675. static struct platform_driver stm32_timer_trigger_driver = {
  676. .probe = stm32_timer_trigger_probe,
  677. .driver = {
  678. .name = "stm32-timer-trigger",
  679. .of_match_table = stm32_trig_of_match,
  680. },
  681. };
  682. module_platform_driver(stm32_timer_trigger_driver);
  683. MODULE_ALIAS("platform: stm32-timer-trigger");
  684. MODULE_DESCRIPTION("STMicroelectronics STM32 Timer Trigger driver");
  685. MODULE_LICENSE("GPL v2");