ti-eqep.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. #define QEPSTS_UPEVNT BIT(7)
  102. #define QEPSTS_FDF BIT(6)
  103. #define QEPSTS_QDF BIT(5)
  104. #define QEPSTS_QDLF BIT(4)
  105. #define QEPSTS_COEF BIT(3)
  106. #define QEPSTS_CDEF BIT(2)
  107. #define QEPSTS_FIMF BIT(1)
  108. #define QEPSTS_PCEF BIT(0)
  109. /* EQEP Inputs */
  110. enum {
  111. TI_EQEP_SIGNAL_QEPA, /* QEPA/XCLK */
  112. TI_EQEP_SIGNAL_QEPB, /* QEPB/XDIR */
  113. };
  114. /* Position Counter Input Modes */
  115. enum ti_eqep_count_func {
  116. TI_EQEP_COUNT_FUNC_QUAD_COUNT,
  117. TI_EQEP_COUNT_FUNC_DIR_COUNT,
  118. TI_EQEP_COUNT_FUNC_UP_COUNT,
  119. TI_EQEP_COUNT_FUNC_DOWN_COUNT,
  120. };
  121. struct ti_eqep_cnt {
  122. struct regmap *regmap32;
  123. struct regmap *regmap16;
  124. };
  125. static int ti_eqep_count_read(struct counter_device *counter,
  126. struct counter_count *count, u64 *val)
  127. {
  128. struct ti_eqep_cnt *priv = counter_priv(counter);
  129. u32 cnt;
  130. regmap_read(priv->regmap32, QPOSCNT, &cnt);
  131. *val = cnt;
  132. return 0;
  133. }
  134. static int ti_eqep_count_write(struct counter_device *counter,
  135. struct counter_count *count, u64 val)
  136. {
  137. struct ti_eqep_cnt *priv = counter_priv(counter);
  138. u32 max;
  139. regmap_read(priv->regmap32, QPOSMAX, &max);
  140. if (val > max)
  141. return -EINVAL;
  142. return regmap_write(priv->regmap32, QPOSCNT, val);
  143. }
  144. static int ti_eqep_function_read(struct counter_device *counter,
  145. struct counter_count *count,
  146. enum counter_function *function)
  147. {
  148. struct ti_eqep_cnt *priv = counter_priv(counter);
  149. u32 qdecctl;
  150. regmap_read(priv->regmap16, QDECCTL, &qdecctl);
  151. switch ((qdecctl & QDECCTL_QSRC) >> QDECCTL_QSRC_SHIFT) {
  152. case TI_EQEP_COUNT_FUNC_QUAD_COUNT:
  153. *function = COUNTER_FUNCTION_QUADRATURE_X4;
  154. break;
  155. case TI_EQEP_COUNT_FUNC_DIR_COUNT:
  156. *function = COUNTER_FUNCTION_PULSE_DIRECTION;
  157. break;
  158. case TI_EQEP_COUNT_FUNC_UP_COUNT:
  159. *function = COUNTER_FUNCTION_INCREASE;
  160. break;
  161. case TI_EQEP_COUNT_FUNC_DOWN_COUNT:
  162. *function = COUNTER_FUNCTION_DECREASE;
  163. break;
  164. }
  165. return 0;
  166. }
  167. static int ti_eqep_function_write(struct counter_device *counter,
  168. struct counter_count *count,
  169. enum counter_function function)
  170. {
  171. struct ti_eqep_cnt *priv = counter_priv(counter);
  172. enum ti_eqep_count_func qsrc;
  173. switch (function) {
  174. case COUNTER_FUNCTION_QUADRATURE_X4:
  175. qsrc = TI_EQEP_COUNT_FUNC_QUAD_COUNT;
  176. break;
  177. case COUNTER_FUNCTION_PULSE_DIRECTION:
  178. qsrc = TI_EQEP_COUNT_FUNC_DIR_COUNT;
  179. break;
  180. case COUNTER_FUNCTION_INCREASE:
  181. qsrc = TI_EQEP_COUNT_FUNC_UP_COUNT;
  182. break;
  183. case COUNTER_FUNCTION_DECREASE:
  184. qsrc = TI_EQEP_COUNT_FUNC_DOWN_COUNT;
  185. break;
  186. default:
  187. /* should never reach this path */
  188. return -EINVAL;
  189. }
  190. return regmap_write_bits(priv->regmap16, QDECCTL, QDECCTL_QSRC,
  191. qsrc << QDECCTL_QSRC_SHIFT);
  192. }
  193. static int ti_eqep_action_read(struct counter_device *counter,
  194. struct counter_count *count,
  195. struct counter_synapse *synapse,
  196. enum counter_synapse_action *action)
  197. {
  198. struct ti_eqep_cnt *priv = counter_priv(counter);
  199. enum counter_function function;
  200. u32 qdecctl;
  201. int err;
  202. err = ti_eqep_function_read(counter, count, &function);
  203. if (err)
  204. return err;
  205. switch (function) {
  206. case COUNTER_FUNCTION_QUADRATURE_X4:
  207. /* In quadrature mode, the rising and falling edge of both
  208. * QEPA and QEPB trigger QCLK.
  209. */
  210. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  211. return 0;
  212. case COUNTER_FUNCTION_PULSE_DIRECTION:
  213. /* In direction-count mode only rising edge of QEPA is counted
  214. * and QEPB gives direction.
  215. */
  216. switch (synapse->signal->id) {
  217. case TI_EQEP_SIGNAL_QEPA:
  218. *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
  219. return 0;
  220. case TI_EQEP_SIGNAL_QEPB:
  221. *action = COUNTER_SYNAPSE_ACTION_NONE;
  222. return 0;
  223. default:
  224. /* should never reach this path */
  225. return -EINVAL;
  226. }
  227. case COUNTER_FUNCTION_INCREASE:
  228. case COUNTER_FUNCTION_DECREASE:
  229. /* In up/down-count modes only QEPA is counted and QEPB is not
  230. * used.
  231. */
  232. switch (synapse->signal->id) {
  233. case TI_EQEP_SIGNAL_QEPA:
  234. err = regmap_read(priv->regmap16, QDECCTL, &qdecctl);
  235. if (err)
  236. return err;
  237. if (qdecctl & QDECCTL_XCR)
  238. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  239. else
  240. *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
  241. return 0;
  242. case TI_EQEP_SIGNAL_QEPB:
  243. *action = COUNTER_SYNAPSE_ACTION_NONE;
  244. return 0;
  245. default:
  246. /* should never reach this path */
  247. return -EINVAL;
  248. }
  249. default:
  250. /* should never reach this path */
  251. return -EINVAL;
  252. }
  253. }
  254. static int ti_eqep_events_configure(struct counter_device *counter)
  255. {
  256. struct ti_eqep_cnt *priv = counter_priv(counter);
  257. struct counter_event_node *event_node;
  258. u32 qeint = 0;
  259. list_for_each_entry(event_node, &counter->events_list, l) {
  260. switch (event_node->event) {
  261. case COUNTER_EVENT_OVERFLOW:
  262. qeint |= QEINT_PCO;
  263. break;
  264. case COUNTER_EVENT_UNDERFLOW:
  265. qeint |= QEINT_PCU;
  266. break;
  267. case COUNTER_EVENT_DIRECTION_CHANGE:
  268. qeint |= QEINT_QDC;
  269. break;
  270. }
  271. }
  272. return regmap_write(priv->regmap16, QEINT, qeint);
  273. }
  274. static int ti_eqep_watch_validate(struct counter_device *counter,
  275. const struct counter_watch *watch)
  276. {
  277. switch (watch->event) {
  278. case COUNTER_EVENT_OVERFLOW:
  279. case COUNTER_EVENT_UNDERFLOW:
  280. case COUNTER_EVENT_DIRECTION_CHANGE:
  281. if (watch->channel != 0)
  282. return -EINVAL;
  283. return 0;
  284. default:
  285. return -EINVAL;
  286. }
  287. }
  288. static const struct counter_ops ti_eqep_counter_ops = {
  289. .count_read = ti_eqep_count_read,
  290. .count_write = ti_eqep_count_write,
  291. .function_read = ti_eqep_function_read,
  292. .function_write = ti_eqep_function_write,
  293. .action_read = ti_eqep_action_read,
  294. .events_configure = ti_eqep_events_configure,
  295. .watch_validate = ti_eqep_watch_validate,
  296. };
  297. static int ti_eqep_position_ceiling_read(struct counter_device *counter,
  298. struct counter_count *count,
  299. u64 *ceiling)
  300. {
  301. struct ti_eqep_cnt *priv = counter_priv(counter);
  302. u32 qposmax;
  303. regmap_read(priv->regmap32, QPOSMAX, &qposmax);
  304. *ceiling = qposmax;
  305. return 0;
  306. }
  307. static int ti_eqep_position_ceiling_write(struct counter_device *counter,
  308. struct counter_count *count,
  309. u64 ceiling)
  310. {
  311. struct ti_eqep_cnt *priv = counter_priv(counter);
  312. if (ceiling != (u32)ceiling)
  313. return -ERANGE;
  314. regmap_write(priv->regmap32, QPOSMAX, ceiling);
  315. return 0;
  316. }
  317. static int ti_eqep_position_enable_read(struct counter_device *counter,
  318. struct counter_count *count, u8 *enable)
  319. {
  320. struct ti_eqep_cnt *priv = counter_priv(counter);
  321. u32 qepctl;
  322. regmap_read(priv->regmap16, QEPCTL, &qepctl);
  323. *enable = !!(qepctl & QEPCTL_PHEN);
  324. return 0;
  325. }
  326. static int ti_eqep_position_enable_write(struct counter_device *counter,
  327. struct counter_count *count, u8 enable)
  328. {
  329. struct ti_eqep_cnt *priv = counter_priv(counter);
  330. regmap_write_bits(priv->regmap16, QEPCTL, QEPCTL_PHEN, enable ? -1 : 0);
  331. return 0;
  332. }
  333. static int ti_eqep_direction_read(struct counter_device *counter,
  334. struct counter_count *count,
  335. enum counter_count_direction *direction)
  336. {
  337. struct ti_eqep_cnt *priv = counter_priv(counter);
  338. u32 qepsts;
  339. regmap_read(priv->regmap16, QEPSTS, &qepsts);
  340. *direction = (qepsts & QEPSTS_QDF) ? COUNTER_COUNT_DIRECTION_FORWARD
  341. : COUNTER_COUNT_DIRECTION_BACKWARD;
  342. return 0;
  343. }
  344. static struct counter_comp ti_eqep_position_ext[] = {
  345. COUNTER_COMP_CEILING(ti_eqep_position_ceiling_read,
  346. ti_eqep_position_ceiling_write),
  347. COUNTER_COMP_ENABLE(ti_eqep_position_enable_read,
  348. ti_eqep_position_enable_write),
  349. COUNTER_COMP_DIRECTION(ti_eqep_direction_read),
  350. };
  351. static struct counter_signal ti_eqep_signals[] = {
  352. [TI_EQEP_SIGNAL_QEPA] = {
  353. .id = TI_EQEP_SIGNAL_QEPA,
  354. .name = "QEPA"
  355. },
  356. [TI_EQEP_SIGNAL_QEPB] = {
  357. .id = TI_EQEP_SIGNAL_QEPB,
  358. .name = "QEPB"
  359. },
  360. };
  361. static const enum counter_function ti_eqep_position_functions[] = {
  362. COUNTER_FUNCTION_QUADRATURE_X4,
  363. COUNTER_FUNCTION_PULSE_DIRECTION,
  364. COUNTER_FUNCTION_INCREASE,
  365. COUNTER_FUNCTION_DECREASE,
  366. };
  367. static const enum counter_synapse_action ti_eqep_position_synapse_actions[] = {
  368. COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
  369. COUNTER_SYNAPSE_ACTION_RISING_EDGE,
  370. COUNTER_SYNAPSE_ACTION_NONE,
  371. };
  372. static struct counter_synapse ti_eqep_position_synapses[] = {
  373. {
  374. .actions_list = ti_eqep_position_synapse_actions,
  375. .num_actions = ARRAY_SIZE(ti_eqep_position_synapse_actions),
  376. .signal = &ti_eqep_signals[TI_EQEP_SIGNAL_QEPA],
  377. },
  378. {
  379. .actions_list = ti_eqep_position_synapse_actions,
  380. .num_actions = ARRAY_SIZE(ti_eqep_position_synapse_actions),
  381. .signal = &ti_eqep_signals[TI_EQEP_SIGNAL_QEPB],
  382. },
  383. };
  384. static struct counter_count ti_eqep_counts[] = {
  385. {
  386. .id = 0,
  387. .name = "QPOSCNT",
  388. .functions_list = ti_eqep_position_functions,
  389. .num_functions = ARRAY_SIZE(ti_eqep_position_functions),
  390. .synapses = ti_eqep_position_synapses,
  391. .num_synapses = ARRAY_SIZE(ti_eqep_position_synapses),
  392. .ext = ti_eqep_position_ext,
  393. .num_ext = ARRAY_SIZE(ti_eqep_position_ext),
  394. },
  395. };
  396. static irqreturn_t ti_eqep_irq_handler(int irq, void *dev_id)
  397. {
  398. struct counter_device *counter = dev_id;
  399. struct ti_eqep_cnt *priv = counter_priv(counter);
  400. u32 qflg;
  401. regmap_read(priv->regmap16, QFLG, &qflg);
  402. if (qflg & QFLG_PCO)
  403. counter_push_event(counter, COUNTER_EVENT_OVERFLOW, 0);
  404. if (qflg & QFLG_PCU)
  405. counter_push_event(counter, COUNTER_EVENT_UNDERFLOW, 0);
  406. if (qflg & QFLG_QDC)
  407. counter_push_event(counter, COUNTER_EVENT_DIRECTION_CHANGE, 0);
  408. regmap_write(priv->regmap16, QCLR, qflg);
  409. return IRQ_HANDLED;
  410. }
  411. static const struct regmap_config ti_eqep_regmap32_config = {
  412. .name = "32-bit",
  413. .reg_bits = 32,
  414. .val_bits = 32,
  415. .reg_stride = 4,
  416. .max_register = QUPRD,
  417. };
  418. static const struct regmap_config ti_eqep_regmap16_config = {
  419. .name = "16-bit",
  420. .reg_bits = 16,
  421. .val_bits = 16,
  422. .reg_stride = 2,
  423. .max_register = QCPRDLAT,
  424. };
  425. static int ti_eqep_probe(struct platform_device *pdev)
  426. {
  427. struct device *dev = &pdev->dev;
  428. struct counter_device *counter;
  429. struct ti_eqep_cnt *priv;
  430. void __iomem *base;
  431. struct clk *clk;
  432. int err, irq;
  433. counter = devm_counter_alloc(dev, sizeof(*priv));
  434. if (!counter)
  435. return -ENOMEM;
  436. priv = counter_priv(counter);
  437. base = devm_platform_ioremap_resource(pdev, 0);
  438. if (IS_ERR(base))
  439. return PTR_ERR(base);
  440. priv->regmap32 = devm_regmap_init_mmio(dev, base,
  441. &ti_eqep_regmap32_config);
  442. if (IS_ERR(priv->regmap32))
  443. return PTR_ERR(priv->regmap32);
  444. priv->regmap16 = devm_regmap_init_mmio(dev, base + 0x24,
  445. &ti_eqep_regmap16_config);
  446. if (IS_ERR(priv->regmap16))
  447. return PTR_ERR(priv->regmap16);
  448. irq = platform_get_irq(pdev, 0);
  449. if (irq < 0)
  450. return irq;
  451. err = devm_request_threaded_irq(dev, irq, NULL, ti_eqep_irq_handler,
  452. IRQF_ONESHOT, dev_name(dev), counter);
  453. if (err < 0)
  454. return dev_err_probe(dev, err, "failed to request IRQ\n");
  455. counter->name = dev_name(dev);
  456. counter->parent = dev;
  457. counter->ops = &ti_eqep_counter_ops;
  458. counter->counts = ti_eqep_counts;
  459. counter->num_counts = ARRAY_SIZE(ti_eqep_counts);
  460. counter->signals = ti_eqep_signals;
  461. counter->num_signals = ARRAY_SIZE(ti_eqep_signals);
  462. platform_set_drvdata(pdev, counter);
  463. /*
  464. * Need to make sure power is turned on. On AM33xx, this comes from the
  465. * parent PWMSS bus driver. On AM17xx, this comes from the PSC power
  466. * domain.
  467. */
  468. pm_runtime_enable(dev);
  469. pm_runtime_get_sync(dev);
  470. clk = devm_clk_get_enabled(dev, NULL);
  471. if (IS_ERR(clk))
  472. return dev_err_probe(dev, PTR_ERR(clk), "failed to enable clock\n");
  473. err = counter_add(counter);
  474. if (err < 0) {
  475. pm_runtime_put_sync(dev);
  476. pm_runtime_disable(dev);
  477. return err;
  478. }
  479. return 0;
  480. }
  481. static void ti_eqep_remove(struct platform_device *pdev)
  482. {
  483. struct counter_device *counter = platform_get_drvdata(pdev);
  484. struct device *dev = &pdev->dev;
  485. counter_unregister(counter);
  486. pm_runtime_put_sync(dev);
  487. pm_runtime_disable(dev);
  488. }
  489. static const struct of_device_id ti_eqep_of_match[] = {
  490. { .compatible = "ti,am3352-eqep", },
  491. { .compatible = "ti,am62-eqep", },
  492. { },
  493. };
  494. MODULE_DEVICE_TABLE(of, ti_eqep_of_match);
  495. static struct platform_driver ti_eqep_driver = {
  496. .probe = ti_eqep_probe,
  497. .remove = ti_eqep_remove,
  498. .driver = {
  499. .name = "ti-eqep-cnt",
  500. .of_match_table = ti_eqep_of_match,
  501. },
  502. };
  503. module_platform_driver(ti_eqep_driver);
  504. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  505. MODULE_DESCRIPTION("TI eQEP counter driver");
  506. MODULE_LICENSE("GPL v2");
  507. MODULE_IMPORT_NS("COUNTER");