stm32-timer-trigger.c 22 KB

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