stm32-timer-trigger.c 22 KB

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