stm32-timer-trigger.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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. regmap_write(priv->regmap, TIM_CNT, val);
  336. return IIO_VAL_INT;
  337. case IIO_CHAN_INFO_SCALE:
  338. /* fixed scale */
  339. return -EINVAL;
  340. }
  341. return -EINVAL;
  342. }
  343. static int stm32_counter_validate_trigger(struct iio_dev *indio_dev,
  344. struct iio_trigger *trig)
  345. {
  346. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  347. const char * const *cur = priv->valids;
  348. unsigned int i = 0;
  349. if (!is_stm32_timer_trigger(trig))
  350. return -EINVAL;
  351. while (cur && *cur) {
  352. if (!strncmp(trig->name, *cur, strlen(trig->name))) {
  353. regmap_update_bits(priv->regmap,
  354. TIM_SMCR, TIM_SMCR_TS,
  355. i << TIM_SMCR_TS_SHIFT);
  356. return 0;
  357. }
  358. cur++;
  359. i++;
  360. }
  361. return -EINVAL;
  362. }
  363. static const struct iio_info stm32_trigger_info = {
  364. .driver_module = THIS_MODULE,
  365. .validate_trigger = stm32_counter_validate_trigger,
  366. .read_raw = stm32_counter_read_raw,
  367. .write_raw = stm32_counter_write_raw
  368. };
  369. static const char *const stm32_trigger_modes[] = {
  370. "trigger",
  371. };
  372. static int stm32_set_trigger_mode(struct iio_dev *indio_dev,
  373. const struct iio_chan_spec *chan,
  374. unsigned int mode)
  375. {
  376. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  377. regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, TIM_SMCR_SMS);
  378. return 0;
  379. }
  380. static int stm32_get_trigger_mode(struct iio_dev *indio_dev,
  381. const struct iio_chan_spec *chan)
  382. {
  383. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  384. u32 smcr;
  385. regmap_read(priv->regmap, TIM_SMCR, &smcr);
  386. return smcr == TIM_SMCR_SMS ? 0 : -EINVAL;
  387. }
  388. static const struct iio_enum stm32_trigger_mode_enum = {
  389. .items = stm32_trigger_modes,
  390. .num_items = ARRAY_SIZE(stm32_trigger_modes),
  391. .set = stm32_set_trigger_mode,
  392. .get = stm32_get_trigger_mode
  393. };
  394. static const char *const stm32_enable_modes[] = {
  395. "always",
  396. "gated",
  397. "triggered",
  398. };
  399. static int stm32_enable_mode2sms(int mode)
  400. {
  401. switch (mode) {
  402. case 0:
  403. return 0;
  404. case 1:
  405. return 5;
  406. case 2:
  407. return 6;
  408. }
  409. return -EINVAL;
  410. }
  411. static int stm32_set_enable_mode(struct iio_dev *indio_dev,
  412. const struct iio_chan_spec *chan,
  413. unsigned int mode)
  414. {
  415. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  416. int sms = stm32_enable_mode2sms(mode);
  417. if (sms < 0)
  418. return sms;
  419. regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, sms);
  420. return 0;
  421. }
  422. static int stm32_sms2enable_mode(int mode)
  423. {
  424. switch (mode) {
  425. case 0:
  426. return 0;
  427. case 5:
  428. return 1;
  429. case 6:
  430. return 2;
  431. }
  432. return -EINVAL;
  433. }
  434. static int stm32_get_enable_mode(struct iio_dev *indio_dev,
  435. const struct iio_chan_spec *chan)
  436. {
  437. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  438. u32 smcr;
  439. regmap_read(priv->regmap, TIM_SMCR, &smcr);
  440. smcr &= TIM_SMCR_SMS;
  441. return stm32_sms2enable_mode(smcr);
  442. }
  443. static const struct iio_enum stm32_enable_mode_enum = {
  444. .items = stm32_enable_modes,
  445. .num_items = ARRAY_SIZE(stm32_enable_modes),
  446. .set = stm32_set_enable_mode,
  447. .get = stm32_get_enable_mode
  448. };
  449. static const char *const stm32_quadrature_modes[] = {
  450. "channel_A",
  451. "channel_B",
  452. "quadrature",
  453. };
  454. static int stm32_set_quadrature_mode(struct iio_dev *indio_dev,
  455. const struct iio_chan_spec *chan,
  456. unsigned int mode)
  457. {
  458. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  459. regmap_update_bits(priv->regmap, TIM_SMCR, TIM_SMCR_SMS, mode + 1);
  460. return 0;
  461. }
  462. static int stm32_get_quadrature_mode(struct iio_dev *indio_dev,
  463. const struct iio_chan_spec *chan)
  464. {
  465. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  466. u32 smcr;
  467. regmap_read(priv->regmap, TIM_SMCR, &smcr);
  468. smcr &= TIM_SMCR_SMS;
  469. return smcr - 1;
  470. }
  471. static const struct iio_enum stm32_quadrature_mode_enum = {
  472. .items = stm32_quadrature_modes,
  473. .num_items = ARRAY_SIZE(stm32_quadrature_modes),
  474. .set = stm32_set_quadrature_mode,
  475. .get = stm32_get_quadrature_mode
  476. };
  477. static const char *const stm32_count_direction_states[] = {
  478. "up",
  479. "down"
  480. };
  481. static int stm32_set_count_direction(struct iio_dev *indio_dev,
  482. const struct iio_chan_spec *chan,
  483. unsigned int mode)
  484. {
  485. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  486. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_DIR, mode);
  487. return 0;
  488. }
  489. static int stm32_get_count_direction(struct iio_dev *indio_dev,
  490. const struct iio_chan_spec *chan)
  491. {
  492. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  493. u32 cr1;
  494. regmap_read(priv->regmap, TIM_CR1, &cr1);
  495. return (cr1 & TIM_CR1_DIR);
  496. }
  497. static const struct iio_enum stm32_count_direction_enum = {
  498. .items = stm32_count_direction_states,
  499. .num_items = ARRAY_SIZE(stm32_count_direction_states),
  500. .set = stm32_set_count_direction,
  501. .get = stm32_get_count_direction
  502. };
  503. static ssize_t stm32_count_get_preset(struct iio_dev *indio_dev,
  504. uintptr_t private,
  505. const struct iio_chan_spec *chan,
  506. char *buf)
  507. {
  508. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  509. u32 arr;
  510. regmap_read(priv->regmap, TIM_ARR, &arr);
  511. return snprintf(buf, PAGE_SIZE, "%u\n", arr);
  512. }
  513. static ssize_t stm32_count_set_preset(struct iio_dev *indio_dev,
  514. uintptr_t private,
  515. const struct iio_chan_spec *chan,
  516. const char *buf, size_t len)
  517. {
  518. struct stm32_timer_trigger *priv = iio_priv(indio_dev);
  519. unsigned int preset;
  520. int ret;
  521. ret = kstrtouint(buf, 0, &preset);
  522. if (ret)
  523. return ret;
  524. regmap_write(priv->regmap, TIM_ARR, preset);
  525. regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, TIM_CR1_ARPE);
  526. return len;
  527. }
  528. static const struct iio_chan_spec_ext_info stm32_trigger_count_info[] = {
  529. {
  530. .name = "preset",
  531. .shared = IIO_SEPARATE,
  532. .read = stm32_count_get_preset,
  533. .write = stm32_count_set_preset
  534. },
  535. IIO_ENUM("count_direction", IIO_SEPARATE, &stm32_count_direction_enum),
  536. IIO_ENUM_AVAILABLE("count_direction", &stm32_count_direction_enum),
  537. IIO_ENUM("quadrature_mode", IIO_SEPARATE, &stm32_quadrature_mode_enum),
  538. IIO_ENUM_AVAILABLE("quadrature_mode", &stm32_quadrature_mode_enum),
  539. IIO_ENUM("enable_mode", IIO_SEPARATE, &stm32_enable_mode_enum),
  540. IIO_ENUM_AVAILABLE("enable_mode", &stm32_enable_mode_enum),
  541. IIO_ENUM("trigger_mode", IIO_SEPARATE, &stm32_trigger_mode_enum),
  542. IIO_ENUM_AVAILABLE("trigger_mode", &stm32_trigger_mode_enum),
  543. {}
  544. };
  545. static const struct iio_chan_spec stm32_trigger_channel = {
  546. .type = IIO_COUNT,
  547. .channel = 0,
  548. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  549. .ext_info = stm32_trigger_count_info,
  550. .indexed = 1
  551. };
  552. static struct stm32_timer_trigger *stm32_setup_counter_device(struct device *dev)
  553. {
  554. struct iio_dev *indio_dev;
  555. int ret;
  556. indio_dev = devm_iio_device_alloc(dev,
  557. sizeof(struct stm32_timer_trigger));
  558. if (!indio_dev)
  559. return NULL;
  560. indio_dev->name = dev_name(dev);
  561. indio_dev->dev.parent = dev;
  562. indio_dev->info = &stm32_trigger_info;
  563. indio_dev->modes = INDIO_HARDWARE_TRIGGERED;
  564. indio_dev->num_channels = 1;
  565. indio_dev->channels = &stm32_trigger_channel;
  566. indio_dev->dev.of_node = dev->of_node;
  567. ret = devm_iio_device_register(dev, indio_dev);
  568. if (ret)
  569. return NULL;
  570. return iio_priv(indio_dev);
  571. }
  572. /**
  573. * is_stm32_timer_trigger
  574. * @trig: trigger to be checked
  575. *
  576. * return true if the trigger is a valid stm32 iio timer trigger
  577. * either return false
  578. */
  579. bool is_stm32_timer_trigger(struct iio_trigger *trig)
  580. {
  581. return (trig->ops == &timer_trigger_ops);
  582. }
  583. EXPORT_SYMBOL(is_stm32_timer_trigger);
  584. static void stm32_timer_detect_trgo2(struct stm32_timer_trigger *priv)
  585. {
  586. u32 val;
  587. /*
  588. * Master mode selection 2 bits can only be written and read back when
  589. * timer supports it.
  590. */
  591. regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS2, TIM_CR2_MMS2);
  592. regmap_read(priv->regmap, TIM_CR2, &val);
  593. regmap_update_bits(priv->regmap, TIM_CR2, TIM_CR2_MMS2, 0);
  594. priv->has_trgo2 = !!val;
  595. }
  596. static int stm32_timer_trigger_probe(struct platform_device *pdev)
  597. {
  598. struct device *dev = &pdev->dev;
  599. struct stm32_timer_trigger *priv;
  600. struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent);
  601. unsigned int index;
  602. int ret;
  603. if (of_property_read_u32(dev->of_node, "reg", &index))
  604. return -EINVAL;
  605. if (index >= ARRAY_SIZE(triggers_table) ||
  606. index >= ARRAY_SIZE(valids_table))
  607. return -EINVAL;
  608. /* Create an IIO device only if we have triggers to be validated */
  609. if (*valids_table[index])
  610. priv = stm32_setup_counter_device(dev);
  611. else
  612. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  613. if (!priv)
  614. return -ENOMEM;
  615. priv->dev = dev;
  616. priv->regmap = ddata->regmap;
  617. priv->clk = ddata->clk;
  618. priv->max_arr = ddata->max_arr;
  619. priv->triggers = triggers_table[index];
  620. priv->valids = valids_table[index];
  621. stm32_timer_detect_trgo2(priv);
  622. ret = stm32_setup_iio_triggers(priv);
  623. if (ret)
  624. return ret;
  625. platform_set_drvdata(pdev, priv);
  626. return 0;
  627. }
  628. static const struct of_device_id stm32_trig_of_match[] = {
  629. { .compatible = "st,stm32-timer-trigger", },
  630. { /* end node */ },
  631. };
  632. MODULE_DEVICE_TABLE(of, stm32_trig_of_match);
  633. static struct platform_driver stm32_timer_trigger_driver = {
  634. .probe = stm32_timer_trigger_probe,
  635. .driver = {
  636. .name = "stm32-timer-trigger",
  637. .of_match_table = stm32_trig_of_match,
  638. },
  639. };
  640. module_platform_driver(stm32_timer_trigger_driver);
  641. MODULE_ALIAS("platform: stm32-timer-trigger");
  642. MODULE_DESCRIPTION("STMicroelectronics STM32 Timer Trigger driver");
  643. MODULE_LICENSE("GPL v2");