stm32-timer-trigger.c 19 KB

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