104-quad-8.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Counter driver for the ACCES 104-QUAD-8
  4. * Copyright (C) 2016 William Breathitt Gray
  5. *
  6. * This driver supports the ACCES 104-QUAD-8 and ACCES 104-QUAD-4.
  7. */
  8. #include <linux/bitfield.h>
  9. #include <linux/bits.h>
  10. #include <linux/counter.h>
  11. #include <linux/device.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/ioport.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/isa.h>
  17. #include <linux/kernel.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/regmap.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/types.h>
  24. #include <linux/unaligned.h>
  25. #define QUAD8_EXTENT 32
  26. static unsigned int base[max_num_isa_dev(QUAD8_EXTENT)];
  27. static unsigned int num_quad8;
  28. module_param_hw_array(base, uint, ioport, &num_quad8, 0);
  29. MODULE_PARM_DESC(base, "ACCES 104-QUAD-8 base addresses");
  30. static unsigned int irq[max_num_isa_dev(QUAD8_EXTENT)];
  31. static unsigned int num_irq;
  32. module_param_hw_array(irq, uint, irq, &num_irq, 0);
  33. MODULE_PARM_DESC(irq, "ACCES 104-QUAD-8 interrupt line numbers");
  34. #define QUAD8_NUM_COUNTERS 8
  35. #define QUAD8_DATA(_channel) ((_channel) * 2)
  36. #define QUAD8_CONTROL(_channel) (QUAD8_DATA(_channel) + 1)
  37. #define QUAD8_INTERRUPT_STATUS 0x10
  38. #define QUAD8_CHANNEL_OPERATION 0x11
  39. #define QUAD8_INDEX_INTERRUPT 0x12
  40. #define QUAD8_INDEX_INPUT_LEVELS 0x16
  41. #define QUAD8_CABLE_STATUS 0x17
  42. /**
  43. * struct quad8 - device private data structure
  44. * @lock: lock to prevent clobbering device states during R/W ops
  45. * @cmr: array of Counter Mode Register states
  46. * @ior: array of Input / Output Control Register states
  47. * @idr: array of Index Control Register states
  48. * @fck_prescaler: array of filter clock prescaler configurations
  49. * @preset: array of preset values
  50. * @cable_fault_enable: differential encoder cable status enable configurations
  51. * @map: regmap for the device
  52. */
  53. struct quad8 {
  54. spinlock_t lock;
  55. u8 cmr[QUAD8_NUM_COUNTERS];
  56. u8 ior[QUAD8_NUM_COUNTERS];
  57. u8 idr[QUAD8_NUM_COUNTERS];
  58. unsigned int fck_prescaler[QUAD8_NUM_COUNTERS];
  59. unsigned int preset[QUAD8_NUM_COUNTERS];
  60. unsigned int cable_fault_enable;
  61. struct regmap *map;
  62. };
  63. static const struct regmap_range quad8_wr_ranges[] = {
  64. regmap_reg_range(0x0, 0xF), regmap_reg_range(0x11, 0x12), regmap_reg_range(0x17, 0x17),
  65. };
  66. static const struct regmap_range quad8_rd_ranges[] = {
  67. regmap_reg_range(0x0, 0x12), regmap_reg_range(0x16, 0x18),
  68. };
  69. static const struct regmap_access_table quad8_wr_table = {
  70. .yes_ranges = quad8_wr_ranges,
  71. .n_yes_ranges = ARRAY_SIZE(quad8_wr_ranges),
  72. };
  73. static const struct regmap_access_table quad8_rd_table = {
  74. .yes_ranges = quad8_rd_ranges,
  75. .n_yes_ranges = ARRAY_SIZE(quad8_rd_ranges),
  76. };
  77. static const struct regmap_config quad8_regmap_config = {
  78. .reg_bits = 8,
  79. .reg_stride = 1,
  80. .val_bits = 8,
  81. .io_port = true,
  82. .wr_table = &quad8_wr_table,
  83. .rd_table = &quad8_rd_table,
  84. };
  85. /* Error flag */
  86. #define FLAG_E BIT(4)
  87. /* Up/Down flag */
  88. #define FLAG_UD BIT(5)
  89. /* Counting up */
  90. #define UP 0x1
  91. #define REGISTER_SELECTION GENMASK(6, 5)
  92. /* Reset and Load Signal Decoders */
  93. #define SELECT_RLD u8_encode_bits(0x0, REGISTER_SELECTION)
  94. /* Counter Mode Register */
  95. #define SELECT_CMR u8_encode_bits(0x1, REGISTER_SELECTION)
  96. /* Input / Output Control Register */
  97. #define SELECT_IOR u8_encode_bits(0x2, REGISTER_SELECTION)
  98. /* Index Control Register */
  99. #define SELECT_IDR u8_encode_bits(0x3, REGISTER_SELECTION)
  100. /*
  101. * Reset and Load Signal Decoders
  102. */
  103. #define RESETS GENMASK(2, 1)
  104. #define LOADS GENMASK(4, 3)
  105. /* Reset Byte Pointer (three byte data pointer) */
  106. #define RESET_BP BIT(0)
  107. /* Reset Borrow Toggle, Carry toggle, Compare toggle, Sign, and Index flags */
  108. #define RESET_BT_CT_CPT_S_IDX u8_encode_bits(0x2, RESETS)
  109. /* Reset Error flag */
  110. #define RESET_E u8_encode_bits(0x3, RESETS)
  111. /* Preset Register to Counter */
  112. #define TRANSFER_PR_TO_CNTR u8_encode_bits(0x1, LOADS)
  113. /* Transfer Counter to Output Latch */
  114. #define TRANSFER_CNTR_TO_OL u8_encode_bits(0x2, LOADS)
  115. /* Transfer Preset Register LSB to FCK Prescaler */
  116. #define TRANSFER_PR0_TO_PSC u8_encode_bits(0x3, LOADS)
  117. /*
  118. * Counter Mode Registers
  119. */
  120. #define COUNT_ENCODING BIT(0)
  121. #define COUNT_MODE GENMASK(2, 1)
  122. #define QUADRATURE_MODE GENMASK(4, 3)
  123. /* Binary count */
  124. #define BINARY u8_encode_bits(0x0, COUNT_ENCODING)
  125. /* Normal count */
  126. #define NORMAL_COUNT 0x0
  127. /* Range Limit */
  128. #define RANGE_LIMIT 0x1
  129. /* Non-recycle count */
  130. #define NON_RECYCLE_COUNT 0x2
  131. /* Modulo-N */
  132. #define MODULO_N 0x3
  133. /* Non-quadrature */
  134. #define NON_QUADRATURE 0x0
  135. /* Quadrature X1 */
  136. #define QUADRATURE_X1 0x1
  137. /* Quadrature X2 */
  138. #define QUADRATURE_X2 0x2
  139. /* Quadrature X4 */
  140. #define QUADRATURE_X4 0x3
  141. /*
  142. * Input/Output Control Register
  143. */
  144. #define AB_GATE BIT(0)
  145. #define LOAD_PIN BIT(1)
  146. #define FLG_PINS GENMASK(4, 3)
  147. /* Disable inputs A and B */
  148. #define DISABLE_AB u8_encode_bits(0x0, AB_GATE)
  149. /* Load Counter input */
  150. #define LOAD_CNTR 0x0
  151. /* FLG1 = CARRY(active low); FLG2 = BORROW(active low) */
  152. #define FLG1_CARRY_FLG2_BORROW 0x0
  153. /* FLG1 = COMPARE(active low); FLG2 = BORROW(active low) */
  154. #define FLG1_COMPARE_FLG2_BORROW 0x1
  155. /* FLG1 = Carry(active low)/Borrow(active low); FLG2 = U/D(active low) flag */
  156. #define FLG1_CARRYBORROW_FLG2_UD 0x2
  157. /* FLG1 = INDX (low pulse at INDEX pin active level); FLG2 = E flag */
  158. #define FLG1_INDX_FLG2_E 0x3
  159. /*
  160. * INDEX CONTROL REGISTERS
  161. */
  162. #define INDEX_MODE BIT(0)
  163. #define INDEX_POLARITY BIT(1)
  164. /* Disable Index mode */
  165. #define DISABLE_INDEX_MODE 0x0
  166. /* Enable Index mode */
  167. #define ENABLE_INDEX_MODE 0x1
  168. /* Negative Index Polarity */
  169. #define NEGATIVE_INDEX_POLARITY 0x0
  170. /* Positive Index Polarity */
  171. #define POSITIVE_INDEX_POLARITY 0x1
  172. /*
  173. * Channel Operation Register
  174. */
  175. #define COUNTERS_OPERATION BIT(0)
  176. #define INTERRUPT_FUNCTION BIT(2)
  177. /* Enable all Counters */
  178. #define ENABLE_COUNTERS u8_encode_bits(0x0, COUNTERS_OPERATION)
  179. /* Reset all Counters */
  180. #define RESET_COUNTERS u8_encode_bits(0x1, COUNTERS_OPERATION)
  181. /* Disable the interrupt function */
  182. #define DISABLE_INTERRUPT_FUNCTION u8_encode_bits(0x0, INTERRUPT_FUNCTION)
  183. /* Enable the interrupt function */
  184. #define ENABLE_INTERRUPT_FUNCTION u8_encode_bits(0x1, INTERRUPT_FUNCTION)
  185. /* Any write to the Channel Operation register clears any pending interrupts */
  186. #define CLEAR_PENDING_INTERRUPTS (ENABLE_COUNTERS | ENABLE_INTERRUPT_FUNCTION)
  187. /* Each Counter is 24 bits wide */
  188. #define LS7267_CNTR_MAX GENMASK(23, 0)
  189. static __always_inline int quad8_control_register_update(struct regmap *const map, u8 *const buf,
  190. const size_t channel, const u8 val,
  191. const u8 field)
  192. {
  193. u8p_replace_bits(&buf[channel], val, field);
  194. return regmap_write(map, QUAD8_CONTROL(channel), buf[channel]);
  195. }
  196. static int quad8_signal_read(struct counter_device *counter,
  197. struct counter_signal *signal,
  198. enum counter_signal_level *level)
  199. {
  200. const struct quad8 *const priv = counter_priv(counter);
  201. int ret;
  202. /* Only Index signal levels can be read */
  203. if (signal->id < 16)
  204. return -EINVAL;
  205. ret = regmap_test_bits(priv->map, QUAD8_INDEX_INPUT_LEVELS, BIT(signal->id - 16));
  206. if (ret < 0)
  207. return ret;
  208. *level = (ret) ? COUNTER_SIGNAL_LEVEL_HIGH : COUNTER_SIGNAL_LEVEL_LOW;
  209. return 0;
  210. }
  211. static int quad8_count_read(struct counter_device *counter,
  212. struct counter_count *count, u64 *val)
  213. {
  214. struct quad8 *const priv = counter_priv(counter);
  215. unsigned long irqflags;
  216. u8 value[3];
  217. int ret;
  218. spin_lock_irqsave(&priv->lock, irqflags);
  219. ret = regmap_write(priv->map, QUAD8_CONTROL(count->id),
  220. SELECT_RLD | RESET_BP | TRANSFER_CNTR_TO_OL);
  221. if (ret)
  222. goto exit_unlock;
  223. ret = regmap_noinc_read(priv->map, QUAD8_DATA(count->id), value, sizeof(value));
  224. exit_unlock:
  225. spin_unlock_irqrestore(&priv->lock, irqflags);
  226. *val = get_unaligned_le24(value);
  227. return ret;
  228. }
  229. static int quad8_preset_register_set(struct quad8 *const priv, const size_t id,
  230. const unsigned long preset)
  231. {
  232. u8 value[3];
  233. int ret;
  234. put_unaligned_le24(preset, value);
  235. ret = regmap_write(priv->map, QUAD8_CONTROL(id), SELECT_RLD | RESET_BP);
  236. if (ret)
  237. return ret;
  238. return regmap_noinc_write(priv->map, QUAD8_DATA(id), value, sizeof(value));
  239. }
  240. static int quad8_flag_register_reset(struct quad8 *const priv, const size_t id)
  241. {
  242. int ret;
  243. ret = regmap_write(priv->map, QUAD8_CONTROL(id), SELECT_RLD | RESET_BT_CT_CPT_S_IDX);
  244. if (ret)
  245. return ret;
  246. return regmap_write(priv->map, QUAD8_CONTROL(id), SELECT_RLD | RESET_E);
  247. }
  248. static int quad8_count_write(struct counter_device *counter,
  249. struct counter_count *count, u64 val)
  250. {
  251. struct quad8 *const priv = counter_priv(counter);
  252. unsigned long irqflags;
  253. int ret;
  254. if (val > LS7267_CNTR_MAX)
  255. return -ERANGE;
  256. spin_lock_irqsave(&priv->lock, irqflags);
  257. /* Counter can only be set via Preset Register */
  258. ret = quad8_preset_register_set(priv, count->id, val);
  259. if (ret)
  260. goto exit_unlock;
  261. ret = regmap_write(priv->map, QUAD8_CONTROL(count->id), SELECT_RLD | TRANSFER_PR_TO_CNTR);
  262. if (ret)
  263. goto exit_unlock;
  264. ret = quad8_flag_register_reset(priv, count->id);
  265. if (ret)
  266. goto exit_unlock;
  267. /* Set Preset Register back to original value */
  268. ret = quad8_preset_register_set(priv, count->id, priv->preset[count->id]);
  269. exit_unlock:
  270. spin_unlock_irqrestore(&priv->lock, irqflags);
  271. return ret;
  272. }
  273. static const enum counter_function quad8_count_functions_list[] = {
  274. COUNTER_FUNCTION_PULSE_DIRECTION,
  275. COUNTER_FUNCTION_QUADRATURE_X1_A,
  276. COUNTER_FUNCTION_QUADRATURE_X2_A,
  277. COUNTER_FUNCTION_QUADRATURE_X4,
  278. };
  279. static int quad8_function_get(const struct quad8 *const priv, const size_t id,
  280. enum counter_function *const function)
  281. {
  282. switch (u8_get_bits(priv->cmr[id], QUADRATURE_MODE)) {
  283. case NON_QUADRATURE:
  284. *function = COUNTER_FUNCTION_PULSE_DIRECTION;
  285. return 0;
  286. case QUADRATURE_X1:
  287. *function = COUNTER_FUNCTION_QUADRATURE_X1_A;
  288. return 0;
  289. case QUADRATURE_X2:
  290. *function = COUNTER_FUNCTION_QUADRATURE_X2_A;
  291. return 0;
  292. case QUADRATURE_X4:
  293. *function = COUNTER_FUNCTION_QUADRATURE_X4;
  294. return 0;
  295. default:
  296. /* should never reach this path */
  297. return -EINVAL;
  298. }
  299. }
  300. static int quad8_function_read(struct counter_device *counter,
  301. struct counter_count *count,
  302. enum counter_function *function)
  303. {
  304. struct quad8 *const priv = counter_priv(counter);
  305. unsigned long irqflags;
  306. int retval;
  307. spin_lock_irqsave(&priv->lock, irqflags);
  308. retval = quad8_function_get(priv, count->id, function);
  309. spin_unlock_irqrestore(&priv->lock, irqflags);
  310. return retval;
  311. }
  312. static int quad8_function_write(struct counter_device *counter,
  313. struct counter_count *count,
  314. enum counter_function function)
  315. {
  316. struct quad8 *const priv = counter_priv(counter);
  317. const int id = count->id;
  318. unsigned long irqflags;
  319. unsigned int mode_cfg;
  320. bool synchronous_mode;
  321. int ret;
  322. switch (function) {
  323. case COUNTER_FUNCTION_PULSE_DIRECTION:
  324. mode_cfg = NON_QUADRATURE;
  325. break;
  326. case COUNTER_FUNCTION_QUADRATURE_X1_A:
  327. mode_cfg = QUADRATURE_X1;
  328. break;
  329. case COUNTER_FUNCTION_QUADRATURE_X2_A:
  330. mode_cfg = QUADRATURE_X2;
  331. break;
  332. case COUNTER_FUNCTION_QUADRATURE_X4:
  333. mode_cfg = QUADRATURE_X4;
  334. break;
  335. default:
  336. /* should never reach this path */
  337. return -EINVAL;
  338. }
  339. spin_lock_irqsave(&priv->lock, irqflags);
  340. /* Synchronous function not supported in non-quadrature mode */
  341. synchronous_mode = u8_get_bits(priv->idr[id], INDEX_MODE) == ENABLE_INDEX_MODE;
  342. if (synchronous_mode && mode_cfg == NON_QUADRATURE) {
  343. ret = quad8_control_register_update(priv->map, priv->idr, id, DISABLE_INDEX_MODE,
  344. INDEX_MODE);
  345. if (ret)
  346. goto exit_unlock;
  347. }
  348. ret = quad8_control_register_update(priv->map, priv->cmr, id, mode_cfg, QUADRATURE_MODE);
  349. exit_unlock:
  350. spin_unlock_irqrestore(&priv->lock, irqflags);
  351. return ret;
  352. }
  353. static int quad8_direction_read(struct counter_device *counter,
  354. struct counter_count *count,
  355. enum counter_count_direction *direction)
  356. {
  357. const struct quad8 *const priv = counter_priv(counter);
  358. unsigned int flag;
  359. int ret;
  360. ret = regmap_read(priv->map, QUAD8_CONTROL(count->id), &flag);
  361. if (ret)
  362. return ret;
  363. *direction = (u8_get_bits(flag, FLAG_UD) == UP) ? COUNTER_COUNT_DIRECTION_FORWARD :
  364. COUNTER_COUNT_DIRECTION_BACKWARD;
  365. return 0;
  366. }
  367. static const enum counter_synapse_action quad8_index_actions_list[] = {
  368. COUNTER_SYNAPSE_ACTION_NONE,
  369. COUNTER_SYNAPSE_ACTION_RISING_EDGE,
  370. };
  371. static const enum counter_synapse_action quad8_synapse_actions_list[] = {
  372. COUNTER_SYNAPSE_ACTION_NONE,
  373. COUNTER_SYNAPSE_ACTION_RISING_EDGE,
  374. COUNTER_SYNAPSE_ACTION_FALLING_EDGE,
  375. COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
  376. };
  377. static int quad8_action_read(struct counter_device *counter,
  378. struct counter_count *count,
  379. struct counter_synapse *synapse,
  380. enum counter_synapse_action *action)
  381. {
  382. struct quad8 *const priv = counter_priv(counter);
  383. unsigned long irqflags;
  384. int err;
  385. enum counter_function function;
  386. const size_t signal_a_id = count->synapses[0].signal->id;
  387. enum counter_count_direction direction;
  388. /* Default action mode */
  389. *action = COUNTER_SYNAPSE_ACTION_NONE;
  390. /* Handle Index signals */
  391. if (synapse->signal->id >= 16) {
  392. if (u8_get_bits(priv->ior[count->id], LOAD_PIN) == LOAD_CNTR)
  393. *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
  394. return 0;
  395. }
  396. spin_lock_irqsave(&priv->lock, irqflags);
  397. /* Get Count function and direction atomically */
  398. err = quad8_function_get(priv, count->id, &function);
  399. if (err) {
  400. spin_unlock_irqrestore(&priv->lock, irqflags);
  401. return err;
  402. }
  403. err = quad8_direction_read(counter, count, &direction);
  404. if (err) {
  405. spin_unlock_irqrestore(&priv->lock, irqflags);
  406. return err;
  407. }
  408. spin_unlock_irqrestore(&priv->lock, irqflags);
  409. /* Determine action mode based on current count function mode */
  410. switch (function) {
  411. case COUNTER_FUNCTION_PULSE_DIRECTION:
  412. if (synapse->signal->id == signal_a_id)
  413. *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
  414. return 0;
  415. case COUNTER_FUNCTION_QUADRATURE_X1_A:
  416. if (synapse->signal->id == signal_a_id) {
  417. if (direction == COUNTER_COUNT_DIRECTION_FORWARD)
  418. *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
  419. else
  420. *action = COUNTER_SYNAPSE_ACTION_FALLING_EDGE;
  421. }
  422. return 0;
  423. case COUNTER_FUNCTION_QUADRATURE_X2_A:
  424. if (synapse->signal->id == signal_a_id)
  425. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  426. return 0;
  427. case COUNTER_FUNCTION_QUADRATURE_X4:
  428. *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
  429. return 0;
  430. default:
  431. /* should never reach this path */
  432. return -EINVAL;
  433. }
  434. }
  435. static int quad8_events_configure(struct counter_device *counter)
  436. {
  437. struct quad8 *const priv = counter_priv(counter);
  438. unsigned long irq_enabled = 0;
  439. unsigned long irqflags;
  440. struct counter_event_node *event_node;
  441. u8 flg_pins;
  442. int ret;
  443. spin_lock_irqsave(&priv->lock, irqflags);
  444. list_for_each_entry(event_node, &counter->events_list, l) {
  445. switch (event_node->event) {
  446. case COUNTER_EVENT_OVERFLOW:
  447. flg_pins = FLG1_CARRY_FLG2_BORROW;
  448. break;
  449. case COUNTER_EVENT_THRESHOLD:
  450. flg_pins = FLG1_COMPARE_FLG2_BORROW;
  451. break;
  452. case COUNTER_EVENT_OVERFLOW_UNDERFLOW:
  453. flg_pins = FLG1_CARRYBORROW_FLG2_UD;
  454. break;
  455. case COUNTER_EVENT_INDEX:
  456. flg_pins = FLG1_INDX_FLG2_E;
  457. break;
  458. default:
  459. /* should never reach this path */
  460. ret = -EINVAL;
  461. goto exit_unlock;
  462. }
  463. /* Enable IRQ line */
  464. irq_enabled |= BIT(event_node->channel);
  465. /* Skip configuration if it is the same as previously set */
  466. if (flg_pins == u8_get_bits(priv->ior[event_node->channel], FLG_PINS))
  467. continue;
  468. /* Save new IRQ function configuration */
  469. ret = quad8_control_register_update(priv->map, priv->ior, event_node->channel,
  470. flg_pins, FLG_PINS);
  471. if (ret)
  472. goto exit_unlock;
  473. }
  474. ret = regmap_write(priv->map, QUAD8_INDEX_INTERRUPT, irq_enabled);
  475. exit_unlock:
  476. spin_unlock_irqrestore(&priv->lock, irqflags);
  477. return ret;
  478. }
  479. static int quad8_watch_validate(struct counter_device *counter,
  480. const struct counter_watch *watch)
  481. {
  482. struct counter_event_node *event_node;
  483. if (watch->channel > QUAD8_NUM_COUNTERS - 1)
  484. return -EINVAL;
  485. switch (watch->event) {
  486. case COUNTER_EVENT_OVERFLOW:
  487. case COUNTER_EVENT_THRESHOLD:
  488. case COUNTER_EVENT_OVERFLOW_UNDERFLOW:
  489. case COUNTER_EVENT_INDEX:
  490. list_for_each_entry(event_node, &counter->next_events_list, l)
  491. if (watch->channel == event_node->channel &&
  492. watch->event != event_node->event)
  493. return -EINVAL;
  494. return 0;
  495. default:
  496. return -EINVAL;
  497. }
  498. }
  499. static const struct counter_ops quad8_ops = {
  500. .signal_read = quad8_signal_read,
  501. .count_read = quad8_count_read,
  502. .count_write = quad8_count_write,
  503. .function_read = quad8_function_read,
  504. .function_write = quad8_function_write,
  505. .action_read = quad8_action_read,
  506. .events_configure = quad8_events_configure,
  507. .watch_validate = quad8_watch_validate,
  508. };
  509. static const char *const quad8_index_polarity_modes[] = {
  510. "negative",
  511. "positive"
  512. };
  513. static int quad8_index_polarity_get(struct counter_device *counter,
  514. struct counter_signal *signal,
  515. u32 *index_polarity)
  516. {
  517. const struct quad8 *const priv = counter_priv(counter);
  518. const size_t channel_id = signal->id - 16;
  519. *index_polarity = u8_get_bits(priv->idr[channel_id], INDEX_POLARITY);
  520. return 0;
  521. }
  522. static int quad8_index_polarity_set(struct counter_device *counter,
  523. struct counter_signal *signal,
  524. u32 index_polarity)
  525. {
  526. struct quad8 *const priv = counter_priv(counter);
  527. const size_t channel_id = signal->id - 16;
  528. unsigned long irqflags;
  529. int ret;
  530. spin_lock_irqsave(&priv->lock, irqflags);
  531. ret = quad8_control_register_update(priv->map, priv->idr, channel_id, index_polarity,
  532. INDEX_POLARITY);
  533. spin_unlock_irqrestore(&priv->lock, irqflags);
  534. return ret;
  535. }
  536. static int quad8_polarity_read(struct counter_device *counter,
  537. struct counter_signal *signal,
  538. enum counter_signal_polarity *polarity)
  539. {
  540. int err;
  541. u32 index_polarity;
  542. err = quad8_index_polarity_get(counter, signal, &index_polarity);
  543. if (err)
  544. return err;
  545. *polarity = (index_polarity == POSITIVE_INDEX_POLARITY) ? COUNTER_SIGNAL_POLARITY_POSITIVE :
  546. COUNTER_SIGNAL_POLARITY_NEGATIVE;
  547. return 0;
  548. }
  549. static int quad8_polarity_write(struct counter_device *counter,
  550. struct counter_signal *signal,
  551. enum counter_signal_polarity polarity)
  552. {
  553. const u32 pol = (polarity == COUNTER_SIGNAL_POLARITY_POSITIVE) ? POSITIVE_INDEX_POLARITY :
  554. NEGATIVE_INDEX_POLARITY;
  555. return quad8_index_polarity_set(counter, signal, pol);
  556. }
  557. static const char *const quad8_synchronous_modes[] = {
  558. "non-synchronous",
  559. "synchronous"
  560. };
  561. static int quad8_synchronous_mode_get(struct counter_device *counter,
  562. struct counter_signal *signal,
  563. u32 *synchronous_mode)
  564. {
  565. const struct quad8 *const priv = counter_priv(counter);
  566. const size_t channel_id = signal->id - 16;
  567. *synchronous_mode = u8_get_bits(priv->idr[channel_id], INDEX_MODE);
  568. return 0;
  569. }
  570. static int quad8_synchronous_mode_set(struct counter_device *counter,
  571. struct counter_signal *signal,
  572. u32 synchronous_mode)
  573. {
  574. struct quad8 *const priv = counter_priv(counter);
  575. const size_t channel_id = signal->id - 16;
  576. u8 quadrature_mode;
  577. unsigned long irqflags;
  578. int ret;
  579. spin_lock_irqsave(&priv->lock, irqflags);
  580. /* Index function must be non-synchronous in non-quadrature mode */
  581. quadrature_mode = u8_get_bits(priv->idr[channel_id], QUADRATURE_MODE);
  582. if (synchronous_mode && quadrature_mode == NON_QUADRATURE) {
  583. ret = -EINVAL;
  584. goto exit_unlock;
  585. }
  586. ret = quad8_control_register_update(priv->map, priv->idr, channel_id, synchronous_mode,
  587. INDEX_MODE);
  588. exit_unlock:
  589. spin_unlock_irqrestore(&priv->lock, irqflags);
  590. return ret;
  591. }
  592. static int quad8_count_floor_read(struct counter_device *counter,
  593. struct counter_count *count, u64 *floor)
  594. {
  595. /* Only a floor of 0 is supported */
  596. *floor = 0;
  597. return 0;
  598. }
  599. static int quad8_count_mode_read(struct counter_device *counter,
  600. struct counter_count *count,
  601. enum counter_count_mode *cnt_mode)
  602. {
  603. const struct quad8 *const priv = counter_priv(counter);
  604. switch (u8_get_bits(priv->cmr[count->id], COUNT_MODE)) {
  605. case NORMAL_COUNT:
  606. *cnt_mode = COUNTER_COUNT_MODE_NORMAL;
  607. break;
  608. case RANGE_LIMIT:
  609. *cnt_mode = COUNTER_COUNT_MODE_RANGE_LIMIT;
  610. break;
  611. case NON_RECYCLE_COUNT:
  612. *cnt_mode = COUNTER_COUNT_MODE_NON_RECYCLE;
  613. break;
  614. case MODULO_N:
  615. *cnt_mode = COUNTER_COUNT_MODE_MODULO_N;
  616. break;
  617. }
  618. return 0;
  619. }
  620. static int quad8_count_mode_write(struct counter_device *counter,
  621. struct counter_count *count,
  622. enum counter_count_mode cnt_mode)
  623. {
  624. struct quad8 *const priv = counter_priv(counter);
  625. unsigned int count_mode;
  626. unsigned long irqflags;
  627. int ret;
  628. switch (cnt_mode) {
  629. case COUNTER_COUNT_MODE_NORMAL:
  630. count_mode = NORMAL_COUNT;
  631. break;
  632. case COUNTER_COUNT_MODE_RANGE_LIMIT:
  633. count_mode = RANGE_LIMIT;
  634. break;
  635. case COUNTER_COUNT_MODE_NON_RECYCLE:
  636. count_mode = NON_RECYCLE_COUNT;
  637. break;
  638. case COUNTER_COUNT_MODE_MODULO_N:
  639. count_mode = MODULO_N;
  640. break;
  641. default:
  642. /* should never reach this path */
  643. return -EINVAL;
  644. }
  645. spin_lock_irqsave(&priv->lock, irqflags);
  646. ret = quad8_control_register_update(priv->map, priv->cmr, count->id, count_mode,
  647. COUNT_MODE);
  648. spin_unlock_irqrestore(&priv->lock, irqflags);
  649. return ret;
  650. }
  651. static int quad8_count_enable_read(struct counter_device *counter,
  652. struct counter_count *count, u8 *enable)
  653. {
  654. const struct quad8 *const priv = counter_priv(counter);
  655. *enable = u8_get_bits(priv->ior[count->id], AB_GATE);
  656. return 0;
  657. }
  658. static int quad8_count_enable_write(struct counter_device *counter,
  659. struct counter_count *count, u8 enable)
  660. {
  661. struct quad8 *const priv = counter_priv(counter);
  662. unsigned long irqflags;
  663. int ret;
  664. spin_lock_irqsave(&priv->lock, irqflags);
  665. ret = quad8_control_register_update(priv->map, priv->ior, count->id, enable, AB_GATE);
  666. spin_unlock_irqrestore(&priv->lock, irqflags);
  667. return ret;
  668. }
  669. static const char *const quad8_noise_error_states[] = {
  670. "No excessive noise is present at the count inputs",
  671. "Excessive noise is present at the count inputs"
  672. };
  673. static int quad8_error_noise_get(struct counter_device *counter,
  674. struct counter_count *count, u32 *noise_error)
  675. {
  676. const struct quad8 *const priv = counter_priv(counter);
  677. unsigned int flag;
  678. int ret;
  679. ret = regmap_read(priv->map, QUAD8_CONTROL(count->id), &flag);
  680. if (ret)
  681. return ret;
  682. *noise_error = u8_get_bits(flag, FLAG_E);
  683. return 0;
  684. }
  685. static int quad8_count_preset_read(struct counter_device *counter,
  686. struct counter_count *count, u64 *preset)
  687. {
  688. const struct quad8 *const priv = counter_priv(counter);
  689. *preset = priv->preset[count->id];
  690. return 0;
  691. }
  692. static int quad8_count_preset_write(struct counter_device *counter,
  693. struct counter_count *count, u64 preset)
  694. {
  695. struct quad8 *const priv = counter_priv(counter);
  696. unsigned long irqflags;
  697. int ret;
  698. if (preset > LS7267_CNTR_MAX)
  699. return -ERANGE;
  700. spin_lock_irqsave(&priv->lock, irqflags);
  701. priv->preset[count->id] = preset;
  702. ret = quad8_preset_register_set(priv, count->id, preset);
  703. spin_unlock_irqrestore(&priv->lock, irqflags);
  704. return ret;
  705. }
  706. static int quad8_count_ceiling_read(struct counter_device *counter,
  707. struct counter_count *count, u64 *ceiling)
  708. {
  709. struct quad8 *const priv = counter_priv(counter);
  710. unsigned long irqflags;
  711. spin_lock_irqsave(&priv->lock, irqflags);
  712. /* Range Limit and Modulo-N count modes use preset value as ceiling */
  713. switch (u8_get_bits(priv->cmr[count->id], COUNT_MODE)) {
  714. case RANGE_LIMIT:
  715. case MODULO_N:
  716. *ceiling = priv->preset[count->id];
  717. break;
  718. default:
  719. *ceiling = LS7267_CNTR_MAX;
  720. break;
  721. }
  722. spin_unlock_irqrestore(&priv->lock, irqflags);
  723. return 0;
  724. }
  725. static int quad8_count_ceiling_write(struct counter_device *counter,
  726. struct counter_count *count, u64 ceiling)
  727. {
  728. struct quad8 *const priv = counter_priv(counter);
  729. unsigned long irqflags;
  730. int ret;
  731. if (ceiling > LS7267_CNTR_MAX)
  732. return -ERANGE;
  733. spin_lock_irqsave(&priv->lock, irqflags);
  734. /* Range Limit and Modulo-N count modes use preset value as ceiling */
  735. switch (u8_get_bits(priv->cmr[count->id], COUNT_MODE)) {
  736. case RANGE_LIMIT:
  737. case MODULO_N:
  738. priv->preset[count->id] = ceiling;
  739. ret = quad8_preset_register_set(priv, count->id, ceiling);
  740. break;
  741. default:
  742. ret = -EINVAL;
  743. break;
  744. }
  745. spin_unlock_irqrestore(&priv->lock, irqflags);
  746. return ret;
  747. }
  748. static int quad8_count_preset_enable_read(struct counter_device *counter,
  749. struct counter_count *count,
  750. u8 *preset_enable)
  751. {
  752. const struct quad8 *const priv = counter_priv(counter);
  753. /* Preset enable is active low in Input/Output Control register */
  754. *preset_enable = !u8_get_bits(priv->ior[count->id], LOAD_PIN);
  755. return 0;
  756. }
  757. static int quad8_count_preset_enable_write(struct counter_device *counter,
  758. struct counter_count *count,
  759. u8 preset_enable)
  760. {
  761. struct quad8 *const priv = counter_priv(counter);
  762. unsigned long irqflags;
  763. int ret;
  764. spin_lock_irqsave(&priv->lock, irqflags);
  765. /* Preset enable is active low in Input/Output Control register */
  766. ret = quad8_control_register_update(priv->map, priv->ior, count->id, !preset_enable,
  767. LOAD_PIN);
  768. spin_unlock_irqrestore(&priv->lock, irqflags);
  769. return ret;
  770. }
  771. static int quad8_signal_cable_fault_read(struct counter_device *counter,
  772. struct counter_signal *signal,
  773. u8 *cable_fault)
  774. {
  775. struct quad8 *const priv = counter_priv(counter);
  776. const size_t channel_id = signal->id / 2;
  777. unsigned long irqflags;
  778. bool disabled;
  779. int ret;
  780. spin_lock_irqsave(&priv->lock, irqflags);
  781. disabled = !(priv->cable_fault_enable & BIT(channel_id));
  782. if (disabled) {
  783. spin_unlock_irqrestore(&priv->lock, irqflags);
  784. return -EINVAL;
  785. }
  786. ret = regmap_test_bits(priv->map, QUAD8_CABLE_STATUS, BIT(channel_id));
  787. if (ret < 0) {
  788. spin_unlock_irqrestore(&priv->lock, irqflags);
  789. return ret;
  790. }
  791. spin_unlock_irqrestore(&priv->lock, irqflags);
  792. /* Logic 0 = cable fault */
  793. *cable_fault = !ret;
  794. return 0;
  795. }
  796. static int quad8_signal_cable_fault_enable_read(struct counter_device *counter,
  797. struct counter_signal *signal,
  798. u8 *enable)
  799. {
  800. const struct quad8 *const priv = counter_priv(counter);
  801. const size_t channel_id = signal->id / 2;
  802. *enable = !!(priv->cable_fault_enable & BIT(channel_id));
  803. return 0;
  804. }
  805. static int quad8_signal_cable_fault_enable_write(struct counter_device *counter,
  806. struct counter_signal *signal,
  807. u8 enable)
  808. {
  809. struct quad8 *const priv = counter_priv(counter);
  810. const size_t channel_id = signal->id / 2;
  811. unsigned long irqflags;
  812. unsigned int cable_fault_enable;
  813. int ret;
  814. spin_lock_irqsave(&priv->lock, irqflags);
  815. if (enable)
  816. priv->cable_fault_enable |= BIT(channel_id);
  817. else
  818. priv->cable_fault_enable &= ~BIT(channel_id);
  819. /* Enable is active low in Differential Encoder Cable Status register */
  820. cable_fault_enable = ~priv->cable_fault_enable;
  821. ret = regmap_write(priv->map, QUAD8_CABLE_STATUS, cable_fault_enable);
  822. spin_unlock_irqrestore(&priv->lock, irqflags);
  823. return ret;
  824. }
  825. static int quad8_signal_fck_prescaler_read(struct counter_device *counter,
  826. struct counter_signal *signal,
  827. u8 *prescaler)
  828. {
  829. const struct quad8 *const priv = counter_priv(counter);
  830. *prescaler = priv->fck_prescaler[signal->id / 2];
  831. return 0;
  832. }
  833. static int quad8_filter_clock_prescaler_set(struct quad8 *const priv, const size_t id,
  834. const u8 prescaler)
  835. {
  836. int ret;
  837. ret = regmap_write(priv->map, QUAD8_CONTROL(id), SELECT_RLD | RESET_BP);
  838. if (ret)
  839. return ret;
  840. ret = regmap_write(priv->map, QUAD8_DATA(id), prescaler);
  841. if (ret)
  842. return ret;
  843. return regmap_write(priv->map, QUAD8_CONTROL(id), SELECT_RLD | TRANSFER_PR0_TO_PSC);
  844. }
  845. static int quad8_signal_fck_prescaler_write(struct counter_device *counter,
  846. struct counter_signal *signal,
  847. u8 prescaler)
  848. {
  849. struct quad8 *const priv = counter_priv(counter);
  850. const size_t channel_id = signal->id / 2;
  851. unsigned long irqflags;
  852. int ret;
  853. spin_lock_irqsave(&priv->lock, irqflags);
  854. priv->fck_prescaler[channel_id] = prescaler;
  855. ret = quad8_filter_clock_prescaler_set(priv, channel_id, prescaler);
  856. spin_unlock_irqrestore(&priv->lock, irqflags);
  857. return ret;
  858. }
  859. static struct counter_comp quad8_signal_ext[] = {
  860. COUNTER_COMP_SIGNAL_BOOL("cable_fault", quad8_signal_cable_fault_read,
  861. NULL),
  862. COUNTER_COMP_SIGNAL_BOOL("cable_fault_enable",
  863. quad8_signal_cable_fault_enable_read,
  864. quad8_signal_cable_fault_enable_write),
  865. COUNTER_COMP_SIGNAL_U8("filter_clock_prescaler",
  866. quad8_signal_fck_prescaler_read,
  867. quad8_signal_fck_prescaler_write)
  868. };
  869. static const enum counter_signal_polarity quad8_polarities[] = {
  870. COUNTER_SIGNAL_POLARITY_POSITIVE,
  871. COUNTER_SIGNAL_POLARITY_NEGATIVE,
  872. };
  873. static DEFINE_COUNTER_AVAILABLE(quad8_polarity_available, quad8_polarities);
  874. static DEFINE_COUNTER_ENUM(quad8_index_pol_enum, quad8_index_polarity_modes);
  875. static DEFINE_COUNTER_ENUM(quad8_synch_mode_enum, quad8_synchronous_modes);
  876. static struct counter_comp quad8_index_ext[] = {
  877. COUNTER_COMP_SIGNAL_ENUM("index_polarity", quad8_index_polarity_get,
  878. quad8_index_polarity_set,
  879. quad8_index_pol_enum),
  880. COUNTER_COMP_POLARITY(quad8_polarity_read, quad8_polarity_write,
  881. quad8_polarity_available),
  882. COUNTER_COMP_SIGNAL_ENUM("synchronous_mode", quad8_synchronous_mode_get,
  883. quad8_synchronous_mode_set,
  884. quad8_synch_mode_enum),
  885. };
  886. #define QUAD8_QUAD_SIGNAL(_id, _name) { \
  887. .id = (_id), \
  888. .name = (_name), \
  889. .ext = quad8_signal_ext, \
  890. .num_ext = ARRAY_SIZE(quad8_signal_ext) \
  891. }
  892. #define QUAD8_INDEX_SIGNAL(_id, _name) { \
  893. .id = (_id), \
  894. .name = (_name), \
  895. .ext = quad8_index_ext, \
  896. .num_ext = ARRAY_SIZE(quad8_index_ext) \
  897. }
  898. static struct counter_signal quad8_signals[] = {
  899. QUAD8_QUAD_SIGNAL(0, "Channel 1 Quadrature A"),
  900. QUAD8_QUAD_SIGNAL(1, "Channel 1 Quadrature B"),
  901. QUAD8_QUAD_SIGNAL(2, "Channel 2 Quadrature A"),
  902. QUAD8_QUAD_SIGNAL(3, "Channel 2 Quadrature B"),
  903. QUAD8_QUAD_SIGNAL(4, "Channel 3 Quadrature A"),
  904. QUAD8_QUAD_SIGNAL(5, "Channel 3 Quadrature B"),
  905. QUAD8_QUAD_SIGNAL(6, "Channel 4 Quadrature A"),
  906. QUAD8_QUAD_SIGNAL(7, "Channel 4 Quadrature B"),
  907. QUAD8_QUAD_SIGNAL(8, "Channel 5 Quadrature A"),
  908. QUAD8_QUAD_SIGNAL(9, "Channel 5 Quadrature B"),
  909. QUAD8_QUAD_SIGNAL(10, "Channel 6 Quadrature A"),
  910. QUAD8_QUAD_SIGNAL(11, "Channel 6 Quadrature B"),
  911. QUAD8_QUAD_SIGNAL(12, "Channel 7 Quadrature A"),
  912. QUAD8_QUAD_SIGNAL(13, "Channel 7 Quadrature B"),
  913. QUAD8_QUAD_SIGNAL(14, "Channel 8 Quadrature A"),
  914. QUAD8_QUAD_SIGNAL(15, "Channel 8 Quadrature B"),
  915. QUAD8_INDEX_SIGNAL(16, "Channel 1 Index"),
  916. QUAD8_INDEX_SIGNAL(17, "Channel 2 Index"),
  917. QUAD8_INDEX_SIGNAL(18, "Channel 3 Index"),
  918. QUAD8_INDEX_SIGNAL(19, "Channel 4 Index"),
  919. QUAD8_INDEX_SIGNAL(20, "Channel 5 Index"),
  920. QUAD8_INDEX_SIGNAL(21, "Channel 6 Index"),
  921. QUAD8_INDEX_SIGNAL(22, "Channel 7 Index"),
  922. QUAD8_INDEX_SIGNAL(23, "Channel 8 Index")
  923. };
  924. #define QUAD8_COUNT_SYNAPSES(_id) { \
  925. { \
  926. .actions_list = quad8_synapse_actions_list, \
  927. .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
  928. .signal = quad8_signals + 2 * (_id) \
  929. }, \
  930. { \
  931. .actions_list = quad8_synapse_actions_list, \
  932. .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
  933. .signal = quad8_signals + 2 * (_id) + 1 \
  934. }, \
  935. { \
  936. .actions_list = quad8_index_actions_list, \
  937. .num_actions = ARRAY_SIZE(quad8_index_actions_list), \
  938. .signal = quad8_signals + 2 * (_id) + 16 \
  939. } \
  940. }
  941. static struct counter_synapse quad8_count_synapses[][3] = {
  942. QUAD8_COUNT_SYNAPSES(0), QUAD8_COUNT_SYNAPSES(1),
  943. QUAD8_COUNT_SYNAPSES(2), QUAD8_COUNT_SYNAPSES(3),
  944. QUAD8_COUNT_SYNAPSES(4), QUAD8_COUNT_SYNAPSES(5),
  945. QUAD8_COUNT_SYNAPSES(6), QUAD8_COUNT_SYNAPSES(7)
  946. };
  947. static const enum counter_count_mode quad8_cnt_modes[] = {
  948. COUNTER_COUNT_MODE_NORMAL,
  949. COUNTER_COUNT_MODE_RANGE_LIMIT,
  950. COUNTER_COUNT_MODE_NON_RECYCLE,
  951. COUNTER_COUNT_MODE_MODULO_N,
  952. };
  953. static DEFINE_COUNTER_AVAILABLE(quad8_count_mode_available, quad8_cnt_modes);
  954. static DEFINE_COUNTER_ENUM(quad8_error_noise_enum, quad8_noise_error_states);
  955. static struct counter_comp quad8_count_ext[] = {
  956. COUNTER_COMP_CEILING(quad8_count_ceiling_read,
  957. quad8_count_ceiling_write),
  958. COUNTER_COMP_FLOOR(quad8_count_floor_read, NULL),
  959. COUNTER_COMP_COUNT_MODE(quad8_count_mode_read, quad8_count_mode_write,
  960. quad8_count_mode_available),
  961. COUNTER_COMP_DIRECTION(quad8_direction_read),
  962. COUNTER_COMP_ENABLE(quad8_count_enable_read, quad8_count_enable_write),
  963. COUNTER_COMP_COUNT_ENUM("error_noise", quad8_error_noise_get, NULL,
  964. quad8_error_noise_enum),
  965. COUNTER_COMP_PRESET(quad8_count_preset_read, quad8_count_preset_write),
  966. COUNTER_COMP_PRESET_ENABLE(quad8_count_preset_enable_read,
  967. quad8_count_preset_enable_write),
  968. };
  969. #define QUAD8_COUNT(_id, _cntname) { \
  970. .id = (_id), \
  971. .name = (_cntname), \
  972. .functions_list = quad8_count_functions_list, \
  973. .num_functions = ARRAY_SIZE(quad8_count_functions_list), \
  974. .synapses = quad8_count_synapses[(_id)], \
  975. .num_synapses = 2, \
  976. .ext = quad8_count_ext, \
  977. .num_ext = ARRAY_SIZE(quad8_count_ext) \
  978. }
  979. static struct counter_count quad8_counts[] = {
  980. QUAD8_COUNT(0, "Channel 1 Count"),
  981. QUAD8_COUNT(1, "Channel 2 Count"),
  982. QUAD8_COUNT(2, "Channel 3 Count"),
  983. QUAD8_COUNT(3, "Channel 4 Count"),
  984. QUAD8_COUNT(4, "Channel 5 Count"),
  985. QUAD8_COUNT(5, "Channel 6 Count"),
  986. QUAD8_COUNT(6, "Channel 7 Count"),
  987. QUAD8_COUNT(7, "Channel 8 Count")
  988. };
  989. static irqreturn_t quad8_irq_handler(int irq, void *private)
  990. {
  991. struct counter_device *counter = private;
  992. struct quad8 *const priv = counter_priv(counter);
  993. unsigned int status;
  994. unsigned long irq_status;
  995. unsigned long channel;
  996. unsigned int flg_pins;
  997. u8 event;
  998. int ret;
  999. ret = regmap_read(priv->map, QUAD8_INTERRUPT_STATUS, &status);
  1000. if (ret)
  1001. return ret;
  1002. if (!status)
  1003. return IRQ_NONE;
  1004. irq_status = status;
  1005. for_each_set_bit(channel, &irq_status, QUAD8_NUM_COUNTERS) {
  1006. flg_pins = u8_get_bits(priv->ior[channel], FLG_PINS);
  1007. switch (flg_pins) {
  1008. case FLG1_CARRY_FLG2_BORROW:
  1009. event = COUNTER_EVENT_OVERFLOW;
  1010. break;
  1011. case FLG1_COMPARE_FLG2_BORROW:
  1012. event = COUNTER_EVENT_THRESHOLD;
  1013. break;
  1014. case FLG1_CARRYBORROW_FLG2_UD:
  1015. event = COUNTER_EVENT_OVERFLOW_UNDERFLOW;
  1016. break;
  1017. case FLG1_INDX_FLG2_E:
  1018. event = COUNTER_EVENT_INDEX;
  1019. break;
  1020. default:
  1021. /* should never reach this path */
  1022. WARN_ONCE(true, "invalid interrupt trigger function %u configured for channel %lu\n",
  1023. flg_pins, channel);
  1024. continue;
  1025. }
  1026. counter_push_event(counter, event, channel);
  1027. }
  1028. ret = regmap_write(priv->map, QUAD8_CHANNEL_OPERATION, CLEAR_PENDING_INTERRUPTS);
  1029. if (ret)
  1030. return ret;
  1031. return IRQ_HANDLED;
  1032. }
  1033. static int quad8_init_counter(struct quad8 *const priv, const size_t channel)
  1034. {
  1035. int ret;
  1036. ret = quad8_filter_clock_prescaler_set(priv, channel, 0);
  1037. if (ret)
  1038. return ret;
  1039. ret = quad8_preset_register_set(priv, channel, 0);
  1040. if (ret)
  1041. return ret;
  1042. ret = quad8_flag_register_reset(priv, channel);
  1043. if (ret)
  1044. return ret;
  1045. /* Binary encoding; Normal count; non-quadrature mode */
  1046. priv->cmr[channel] = SELECT_CMR | BINARY | u8_encode_bits(NORMAL_COUNT, COUNT_MODE) |
  1047. u8_encode_bits(NON_QUADRATURE, QUADRATURE_MODE);
  1048. ret = regmap_write(priv->map, QUAD8_CONTROL(channel), priv->cmr[channel]);
  1049. if (ret)
  1050. return ret;
  1051. /* Disable A and B inputs; preset on index; FLG1 as Carry */
  1052. priv->ior[channel] = SELECT_IOR | DISABLE_AB | u8_encode_bits(LOAD_CNTR, LOAD_PIN) |
  1053. u8_encode_bits(FLG1_CARRY_FLG2_BORROW, FLG_PINS);
  1054. ret = regmap_write(priv->map, QUAD8_CONTROL(channel), priv->ior[channel]);
  1055. if (ret)
  1056. return ret;
  1057. /* Disable index function; negative index polarity */
  1058. priv->idr[channel] = SELECT_IDR | u8_encode_bits(DISABLE_INDEX_MODE, INDEX_MODE) |
  1059. u8_encode_bits(NEGATIVE_INDEX_POLARITY, INDEX_POLARITY);
  1060. return regmap_write(priv->map, QUAD8_CONTROL(channel), priv->idr[channel]);
  1061. }
  1062. static int quad8_probe(struct device *dev, unsigned int id)
  1063. {
  1064. struct counter_device *counter;
  1065. struct quad8 *priv;
  1066. void __iomem *regs;
  1067. unsigned long i;
  1068. int ret;
  1069. if (!devm_request_region(dev, base[id], QUAD8_EXTENT, dev_name(dev))) {
  1070. dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
  1071. base[id], base[id] + QUAD8_EXTENT);
  1072. return -EBUSY;
  1073. }
  1074. counter = devm_counter_alloc(dev, sizeof(*priv));
  1075. if (!counter)
  1076. return -ENOMEM;
  1077. priv = counter_priv(counter);
  1078. regs = devm_ioport_map(dev, base[id], QUAD8_EXTENT);
  1079. if (!regs)
  1080. return -ENOMEM;
  1081. priv->map = devm_regmap_init_mmio(dev, regs, &quad8_regmap_config);
  1082. if (IS_ERR(priv->map))
  1083. return dev_err_probe(dev, PTR_ERR(priv->map),
  1084. "Unable to initialize register map\n");
  1085. /* Initialize Counter device and driver data */
  1086. counter->name = dev_name(dev);
  1087. counter->parent = dev;
  1088. counter->ops = &quad8_ops;
  1089. counter->counts = quad8_counts;
  1090. counter->num_counts = ARRAY_SIZE(quad8_counts);
  1091. counter->signals = quad8_signals;
  1092. counter->num_signals = ARRAY_SIZE(quad8_signals);
  1093. spin_lock_init(&priv->lock);
  1094. /* Reset Index/Interrupt Register */
  1095. ret = regmap_write(priv->map, QUAD8_INDEX_INTERRUPT, 0x00);
  1096. if (ret)
  1097. return ret;
  1098. /* Reset all counters and disable interrupt function */
  1099. ret = regmap_write(priv->map, QUAD8_CHANNEL_OPERATION,
  1100. RESET_COUNTERS | DISABLE_INTERRUPT_FUNCTION);
  1101. if (ret)
  1102. return ret;
  1103. /* Set initial configuration for all counters */
  1104. for (i = 0; i < QUAD8_NUM_COUNTERS; i++) {
  1105. ret = quad8_init_counter(priv, i);
  1106. if (ret)
  1107. return ret;
  1108. }
  1109. /* Disable Differential Encoder Cable Status for all channels */
  1110. ret = regmap_write(priv->map, QUAD8_CABLE_STATUS, GENMASK(7, 0));
  1111. if (ret)
  1112. return ret;
  1113. /* Enable all counters and enable interrupt function */
  1114. ret = regmap_write(priv->map, QUAD8_CHANNEL_OPERATION,
  1115. ENABLE_COUNTERS | ENABLE_INTERRUPT_FUNCTION);
  1116. if (ret)
  1117. return ret;
  1118. ret = devm_request_irq(&counter->dev, irq[id], quad8_irq_handler,
  1119. IRQF_SHARED, counter->name, counter);
  1120. if (ret)
  1121. return ret;
  1122. ret = devm_counter_add(dev, counter);
  1123. if (ret < 0)
  1124. return dev_err_probe(dev, ret, "Failed to add counter\n");
  1125. return 0;
  1126. }
  1127. static struct isa_driver quad8_driver = {
  1128. .probe = quad8_probe,
  1129. .driver = {
  1130. .name = "104-quad-8"
  1131. }
  1132. };
  1133. module_isa_driver_with_irq(quad8_driver, num_quad8, num_irq);
  1134. MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
  1135. MODULE_DESCRIPTION("ACCES 104-QUAD-8 driver");
  1136. MODULE_LICENSE("GPL v2");
  1137. MODULE_IMPORT_NS(COUNTER);