ti-eqep.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2019 David Lechner <david@lechnology.com>
  4. *
  5. * Counter driver for Texas Instruments Enhanced Quadrature Encoder Pulse (eQEP)
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/clk.h>
  9. #include <linux/counter.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/regmap.h>
  17. #include <linux/types.h>
  18. /* 32-bit registers */
  19. #define QPOSCNT 0x0
  20. #define QPOSINIT 0x4
  21. #define QPOSMAX 0x8
  22. #define QPOSCMP 0xc
  23. #define QPOSILAT 0x10
  24. #define QPOSSLAT 0x14
  25. #define QPOSLAT 0x18
  26. #define QUTMR 0x1c
  27. #define QUPRD 0x20
  28. /* 16-bit registers */
  29. #define QWDTMR 0x0 /* 0x24 */
  30. #define QWDPRD 0x2 /* 0x26 */
  31. #define QDECCTL 0x4 /* 0x28 */
  32. #define QEPCTL 0x6 /* 0x2a */
  33. #define QCAPCTL 0x8 /* 0x2c */
  34. #define QPOSCTL 0xa /* 0x2e */
  35. #define QEINT 0xc /* 0x30 */
  36. #define QFLG 0xe /* 0x32 */
  37. #define QCLR 0x10 /* 0x34 */
  38. #define QFRC 0x12 /* 0x36 */
  39. #define QEPSTS 0x14 /* 0x38 */
  40. #define QCTMR 0x16 /* 0x3a */
  41. #define QCPRD 0x18 /* 0x3c */
  42. #define QCTMRLAT 0x1a /* 0x3e */
  43. #define QCPRDLAT 0x1c /* 0x40 */
  44. #define QDECCTL_QSRC_SHIFT 14
  45. #define QDECCTL_QSRC GENMASK(15, 14)
  46. #define QDECCTL_SOEN BIT(13)
  47. #define QDECCTL_SPSEL BIT(12)
  48. #define QDECCTL_XCR BIT(11)
  49. #define QDECCTL_SWAP BIT(10)
  50. #define QDECCTL_IGATE BIT(9)
  51. #define QDECCTL_QAP BIT(8)
  52. #define QDECCTL_QBP BIT(7)
  53. #define QDECCTL_QIP BIT(6)
  54. #define QDECCTL_QSP BIT(5)
  55. #define QEPCTL_FREE_SOFT GENMASK(15, 14)
  56. #define QEPCTL_PCRM GENMASK(13, 12)
  57. #define QEPCTL_SEI GENMASK(11, 10)
  58. #define QEPCTL_IEI GENMASK(9, 8)
  59. #define QEPCTL_SWI BIT(7)
  60. #define QEPCTL_SEL BIT(6)
  61. #define QEPCTL_IEL GENMASK(5, 4)
  62. #define QEPCTL_PHEN BIT(3)
  63. #define QEPCTL_QCLM BIT(2)
  64. #define QEPCTL_UTE BIT(1)
  65. #define QEPCTL_WDE BIT(0)
  66. #define QEINT_UTO BIT(11)
  67. #define QEINT_IEL BIT(10)
  68. #define QEINT_SEL BIT(9)
  69. #define QEINT_PCM BIT(8)
  70. #define QEINT_PCR BIT(7)
  71. #define QEINT_PCO BIT(6)
  72. #define QEINT_PCU BIT(5)
  73. #define QEINT_WTO BIT(4)
  74. #define QEINT_QDC BIT(3)
  75. #define QEINT_PHE BIT(2)
  76. #define QEINT_PCE BIT(1)
  77. #define QFLG_UTO BIT(11)
  78. #define QFLG_IEL BIT(10)
  79. #define QFLG_SEL BIT(9)
  80. #define QFLG_PCM BIT(8)
  81. #define QFLG_PCR BIT(7)
  82. #define QFLG_PCO BIT(6)
  83. #define QFLG_PCU BIT(5)
  84. #define QFLG_WTO BIT(4)
  85. #define QFLG_QDC BIT(3)
  86. #define QFLG_PHE BIT(2)
  87. #define QFLG_PCE BIT(1)
  88. #define QFLG_INT BIT(0)
  89. #define QCLR_UTO BIT(11)
  90. #define QCLR_IEL BIT(10)
  91. #define QCLR_SEL BIT(9)
  92. #define QCLR_PCM BIT(8)
  93. #define QCLR_PCR BIT(7)
  94. #define QCLR_PCO BIT(6)
  95. #define QCLR_PCU BIT(5)
  96. #define QCLR_WTO BIT(4)
  97. #define QCLR_QDC BIT(3)
  98. #define QCLR_PHE BIT(2)
  99. #define QCLR_PCE BIT(1)
  100. #define QCLR_INT BIT(0)
  101. /* EQEP Inputs */
  102. enum {
  103. TI_EQEP_SIGNAL_QEPA, /* QEPA/XCLK */
  104. TI_EQEP_SIGNAL_QEPB, /* QEPB/XDIR */
  105. };
  106. /* Position Counter Input Modes */
  107. enum ti_eqep_count_func {
  108. TI_EQEP_COUNT_FUNC_QUAD_COUNT,
  109. TI_EQEP_COUNT_FUNC_DIR_COUNT,
  110. TI_EQEP_COUNT_FUNC_UP_COUNT,
  111. TI_EQEP_COUNT_FUNC_DOWN_COUNT,
  112. };
  113. struct ti_eqep_cnt {
  114. struct regmap *regmap32;
  115. struct regmap *regmap16;
  116. };
  117. static int ti_eqep_count_read(struct counter_device *counter,
  118. struct counter_count *count, u64 *val)
  119. {
  120. struct ti_eqep_cnt *priv = counter_priv(counter);
  121. u32 cnt;
  122. regmap_read(priv->regmap32, QPOSCNT, &cnt);
  123. *val = cnt;
  124. return 0;
  125. }
  126. static int ti_eqep_count_write(struct counter_device *counter,
  127. struct counter_count *count, u64 val)
  128. {
  129. struct ti_eqep_cnt *priv = counter_priv(counter);
  130. u32 max;
  131. regmap_read(priv->regmap32, QPOSMAX, &max);
  132. if (val > max)
  133. return -EINVAL;
  134. return regmap_write(priv->regmap32, QPOSCNT, val);
  135. }
  136. static int ti_eqep_function_read(struct counter_device *counter,
  137. struct counter_count *count,
  138. enum counter_function *function)
  139. {
  140. struct ti_eqep_cnt *priv = counter_priv(counter);
  141. u32 qdecctl;
  142. regmap_read(priv->regmap16, QDECCTL, &qdecctl);
  143. switch ((qdecctl & QDECCTL_QSRC) >> QDECCTL_QSRC_SHIFT) {
  144. case TI_EQEP_COUNT_FUNC_QUAD_COUNT:
  145. *function = COUNTER_FUNCTION_QUADRATURE_X4;
  146. break;
  147. case TI_EQEP_COUNT_FUNC_DIR_COUNT:
  148. *function = COUNTER_FUNCTION_PULSE_DIRECTION;
  149. break;
  150. case TI_EQEP_COUNT_FUNC_UP_COUNT:
  151. *function = COUNTER_FUNCTION_INCREASE;
  152. break;
  153. case TI_EQEP_COUNT_FUNC_DOWN_COUNT:
  154. *function = COUNTER_FUNCTION_DECREASE;
  155. break;
  156. }
  157. return 0;
  158. }
  159. static int ti_eqep_function_write(struct counter_device *counter,
  160. struct counter_count *count,
  161. enum counter_function function)
  162. {
  163. struct ti_eqep_cnt *priv = counter_priv(counter);
  164. enum ti_eqep_count_func qsrc;
  165. switch (function) {
  166. case COUNTER_FUNCTION_QUADRATURE_X4:
  167. qsrc = TI_EQEP_COUNT_FUNC_QUAD_COUNT;
  168. break;
  169. case COUNTER_FUNCTION_PULSE_DIRECTION:
  170. qsrc = TI_EQEP_COUNT_FUNC_DIR_COUNT;
  171. break;
  172. case COUNTER_FUNCTION_INCREASE:
  173. qsrc = TI_EQEP_COUNT_FUNC_UP_COUNT;
  174. break;
  175. case COUNTER_FUNCTION_DECREASE:
  176. qsrc = TI_EQEP_COUNT_FUNC_DOWN_COUNT;
  177. break;
  178. default:
  179. /* should never reach this path */
  180. return -EINVAL;
  181. }
  182. return regmap_write_bits(priv->regmap16, QDECCTL, QDECCTL_QSRC,
  183. qsrc << QDECCTL_QSRC_SHIFT);
  184. }
  185. static int ti_eqep_action_read(struct counter_device *counter,
  186. struct counter_count *count,
  187. struct counter_synapse *synapse,
  188. enum counter_synapse_action *action)
  189. {
  190. struct ti_eqep_cnt *priv = counter_priv(counter);
  191. enum counter_function function;
  192. u32 qdecctl;
  193. int err;
  194. err = ti_eqep_function_read(counter, count, &function);
  195. if (err)
  196. return err;
  197. switch (function) {
  198. case COUNTER_FUNCTION_QUADRATURE_X4:
  199. /* In quadrature mode, the rising and falling edge of both
  200. * QEPA and QEPB trigger QCLK.
  201. */
  202. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  203. return 0;
  204. case COUNTER_FUNCTION_PULSE_DIRECTION:
  205. /* In direction-count mode only rising edge of QEPA is counted
  206. * and QEPB gives direction.
  207. */
  208. switch (synapse->signal->id) {
  209. case TI_EQEP_SIGNAL_QEPA:
  210. *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
  211. return 0;
  212. case TI_EQEP_SIGNAL_QEPB:
  213. *action = COUNTER_SYNAPSE_ACTION_NONE;
  214. return 0;
  215. default:
  216. /* should never reach this path */
  217. return -EINVAL;
  218. }
  219. case COUNTER_FUNCTION_INCREASE:
  220. case COUNTER_FUNCTION_DECREASE:
  221. /* In up/down-count modes only QEPA is counted and QEPB is not
  222. * used.
  223. */
  224. switch (synapse->signal->id) {
  225. case TI_EQEP_SIGNAL_QEPA:
  226. err = regmap_read(priv->regmap16, QDECCTL, &qdecctl);
  227. if (err)
  228. return err;
  229. if (qdecctl & QDECCTL_XCR)
  230. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  231. else
  232. *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
  233. return 0;
  234. case TI_EQEP_SIGNAL_QEPB:
  235. *action = COUNTER_SYNAPSE_ACTION_NONE;
  236. return 0;
  237. default:
  238. /* should never reach this path */
  239. return -EINVAL;
  240. }
  241. default:
  242. /* should never reach this path */
  243. return -EINVAL;
  244. }
  245. }
  246. static int ti_eqep_events_configure(struct counter_device *counter)
  247. {
  248. struct ti_eqep_cnt *priv = counter_priv(counter);
  249. struct counter_event_node *event_node;
  250. u32 qeint = 0;
  251. list_for_each_entry(event_node, &counter->events_list, l) {
  252. switch (event_node->event) {
  253. case COUNTER_EVENT_OVERFLOW:
  254. qeint |= QEINT_PCO;
  255. break;
  256. case COUNTER_EVENT_UNDERFLOW:
  257. qeint |= QEINT_PCU;
  258. break;
  259. }
  260. }
  261. return regmap_write(priv->regmap16, QEINT, qeint);
  262. }
  263. static int ti_eqep_watch_validate(struct counter_device *counter,
  264. const struct counter_watch *watch)
  265. {
  266. switch (watch->event) {
  267. case COUNTER_EVENT_OVERFLOW:
  268. case COUNTER_EVENT_UNDERFLOW:
  269. if (watch->channel != 0)
  270. return -EINVAL;
  271. return 0;
  272. default:
  273. return -EINVAL;
  274. }
  275. }
  276. static const struct counter_ops ti_eqep_counter_ops = {
  277. .count_read = ti_eqep_count_read,
  278. .count_write = ti_eqep_count_write,
  279. .function_read = ti_eqep_function_read,
  280. .function_write = ti_eqep_function_write,
  281. .action_read = ti_eqep_action_read,
  282. .events_configure = ti_eqep_events_configure,
  283. .watch_validate = ti_eqep_watch_validate,
  284. };
  285. static int ti_eqep_position_ceiling_read(struct counter_device *counter,
  286. struct counter_count *count,
  287. u64 *ceiling)
  288. {
  289. struct ti_eqep_cnt *priv = counter_priv(counter);
  290. u32 qposmax;
  291. regmap_read(priv->regmap32, QPOSMAX, &qposmax);
  292. *ceiling = qposmax;
  293. return 0;
  294. }
  295. static int ti_eqep_position_ceiling_write(struct counter_device *counter,
  296. struct counter_count *count,
  297. u64 ceiling)
  298. {
  299. struct ti_eqep_cnt *priv = counter_priv(counter);
  300. if (ceiling != (u32)ceiling)
  301. return -ERANGE;
  302. regmap_write(priv->regmap32, QPOSMAX, ceiling);
  303. return 0;
  304. }
  305. static int ti_eqep_position_enable_read(struct counter_device *counter,
  306. struct counter_count *count, u8 *enable)
  307. {
  308. struct ti_eqep_cnt *priv = counter_priv(counter);
  309. u32 qepctl;
  310. regmap_read(priv->regmap16, QEPCTL, &qepctl);
  311. *enable = !!(qepctl & QEPCTL_PHEN);
  312. return 0;
  313. }
  314. static int ti_eqep_position_enable_write(struct counter_device *counter,
  315. struct counter_count *count, u8 enable)
  316. {
  317. struct ti_eqep_cnt *priv = counter_priv(counter);
  318. regmap_write_bits(priv->regmap16, QEPCTL, QEPCTL_PHEN, enable ? -1 : 0);
  319. return 0;
  320. }
  321. static struct counter_comp ti_eqep_position_ext[] = {
  322. COUNTER_COMP_CEILING(ti_eqep_position_ceiling_read,
  323. ti_eqep_position_ceiling_write),
  324. COUNTER_COMP_ENABLE(ti_eqep_position_enable_read,
  325. ti_eqep_position_enable_write),
  326. };
  327. static struct counter_signal ti_eqep_signals[] = {
  328. [TI_EQEP_SIGNAL_QEPA] = {
  329. .id = TI_EQEP_SIGNAL_QEPA,
  330. .name = "QEPA"
  331. },
  332. [TI_EQEP_SIGNAL_QEPB] = {
  333. .id = TI_EQEP_SIGNAL_QEPB,
  334. .name = "QEPB"
  335. },
  336. };
  337. static const enum counter_function ti_eqep_position_functions[] = {
  338. COUNTER_FUNCTION_QUADRATURE_X4,
  339. COUNTER_FUNCTION_PULSE_DIRECTION,
  340. COUNTER_FUNCTION_INCREASE,
  341. COUNTER_FUNCTION_DECREASE,
  342. };
  343. static const enum counter_synapse_action ti_eqep_position_synapse_actions[] = {
  344. COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
  345. COUNTER_SYNAPSE_ACTION_RISING_EDGE,
  346. COUNTER_SYNAPSE_ACTION_NONE,
  347. };
  348. static struct counter_synapse ti_eqep_position_synapses[] = {
  349. {
  350. .actions_list = ti_eqep_position_synapse_actions,
  351. .num_actions = ARRAY_SIZE(ti_eqep_position_synapse_actions),
  352. .signal = &ti_eqep_signals[TI_EQEP_SIGNAL_QEPA],
  353. },
  354. {
  355. .actions_list = ti_eqep_position_synapse_actions,
  356. .num_actions = ARRAY_SIZE(ti_eqep_position_synapse_actions),
  357. .signal = &ti_eqep_signals[TI_EQEP_SIGNAL_QEPB],
  358. },
  359. };
  360. static struct counter_count ti_eqep_counts[] = {
  361. {
  362. .id = 0,
  363. .name = "QPOSCNT",
  364. .functions_list = ti_eqep_position_functions,
  365. .num_functions = ARRAY_SIZE(ti_eqep_position_functions),
  366. .synapses = ti_eqep_position_synapses,
  367. .num_synapses = ARRAY_SIZE(ti_eqep_position_synapses),
  368. .ext = ti_eqep_position_ext,
  369. .num_ext = ARRAY_SIZE(ti_eqep_position_ext),
  370. },
  371. };
  372. static irqreturn_t ti_eqep_irq_handler(int irq, void *dev_id)
  373. {
  374. struct counter_device *counter = dev_id;
  375. struct ti_eqep_cnt *priv = counter_priv(counter);
  376. u32 qflg;
  377. regmap_read(priv->regmap16, QFLG, &qflg);
  378. if (qflg & QFLG_PCO)
  379. counter_push_event(counter, COUNTER_EVENT_OVERFLOW, 0);
  380. if (qflg & QFLG_PCU)
  381. counter_push_event(counter, COUNTER_EVENT_UNDERFLOW, 0);
  382. regmap_write(priv->regmap16, QCLR, qflg);
  383. return IRQ_HANDLED;
  384. }
  385. static const struct regmap_config ti_eqep_regmap32_config = {
  386. .name = "32-bit",
  387. .reg_bits = 32,
  388. .val_bits = 32,
  389. .reg_stride = 4,
  390. .max_register = QUPRD,
  391. };
  392. static const struct regmap_config ti_eqep_regmap16_config = {
  393. .name = "16-bit",
  394. .reg_bits = 16,
  395. .val_bits = 16,
  396. .reg_stride = 2,
  397. .max_register = QCPRDLAT,
  398. };
  399. static int ti_eqep_probe(struct platform_device *pdev)
  400. {
  401. struct device *dev = &pdev->dev;
  402. struct counter_device *counter;
  403. struct ti_eqep_cnt *priv;
  404. void __iomem *base;
  405. struct clk *clk;
  406. int err, irq;
  407. counter = devm_counter_alloc(dev, sizeof(*priv));
  408. if (!counter)
  409. return -ENOMEM;
  410. priv = counter_priv(counter);
  411. base = devm_platform_ioremap_resource(pdev, 0);
  412. if (IS_ERR(base))
  413. return PTR_ERR(base);
  414. priv->regmap32 = devm_regmap_init_mmio(dev, base,
  415. &ti_eqep_regmap32_config);
  416. if (IS_ERR(priv->regmap32))
  417. return PTR_ERR(priv->regmap32);
  418. priv->regmap16 = devm_regmap_init_mmio(dev, base + 0x24,
  419. &ti_eqep_regmap16_config);
  420. if (IS_ERR(priv->regmap16))
  421. return PTR_ERR(priv->regmap16);
  422. irq = platform_get_irq(pdev, 0);
  423. if (irq < 0)
  424. return irq;
  425. err = devm_request_threaded_irq(dev, irq, NULL, ti_eqep_irq_handler,
  426. IRQF_ONESHOT, dev_name(dev), counter);
  427. if (err < 0)
  428. return dev_err_probe(dev, err, "failed to request IRQ\n");
  429. counter->name = dev_name(dev);
  430. counter->parent = dev;
  431. counter->ops = &ti_eqep_counter_ops;
  432. counter->counts = ti_eqep_counts;
  433. counter->num_counts = ARRAY_SIZE(ti_eqep_counts);
  434. counter->signals = ti_eqep_signals;
  435. counter->num_signals = ARRAY_SIZE(ti_eqep_signals);
  436. platform_set_drvdata(pdev, counter);
  437. /*
  438. * Need to make sure power is turned on. On AM33xx, this comes from the
  439. * parent PWMSS bus driver. On AM17xx, this comes from the PSC power
  440. * domain.
  441. */
  442. pm_runtime_enable(dev);
  443. pm_runtime_get_sync(dev);
  444. clk = devm_clk_get_enabled(dev, NULL);
  445. if (IS_ERR(clk))
  446. return dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n");
  447. err = counter_add(counter);
  448. if (err < 0) {
  449. pm_runtime_put_sync(dev);
  450. pm_runtime_disable(dev);
  451. return err;
  452. }
  453. return 0;
  454. }
  455. static void ti_eqep_remove(struct platform_device *pdev)
  456. {
  457. struct counter_device *counter = platform_get_drvdata(pdev);
  458. struct device *dev = &pdev->dev;
  459. counter_unregister(counter);
  460. pm_runtime_put_sync(dev);
  461. pm_runtime_disable(dev);
  462. }
  463. static const struct of_device_id ti_eqep_of_match[] = {
  464. { .compatible = "ti,am3352-eqep", },
  465. { .compatible = "ti,am62-eqep", },
  466. { },
  467. };
  468. MODULE_DEVICE_TABLE(of, ti_eqep_of_match);
  469. static struct platform_driver ti_eqep_driver = {
  470. .probe = ti_eqep_probe,
  471. .remove_new = ti_eqep_remove,
  472. .driver = {
  473. .name = "ti-eqep-cnt",
  474. .of_match_table = ti_eqep_of_match,
  475. },
  476. };
  477. module_platform_driver(ti_eqep_driver);
  478. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  479. MODULE_DESCRIPTION("TI eQEP counter driver");
  480. MODULE_LICENSE("GPL v2");
  481. MODULE_IMPORT_NS(COUNTER);