ti-eqep.c 11 KB

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