pinctrl-ark.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * arkmicro pinctrl driver
  3. *
  4. * Licensed under GPLv2 or later.
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/err.h>
  8. #include <linux/init.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/of_address.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/slab.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/pinctrl/machine.h>
  18. #include <linux/pinctrl/pinconf.h>
  19. #include <linux/pinctrl/pinctrl.h>
  20. #include <linux/pinctrl/pinmux.h>
  21. #include "core.h"
  22. #define MAX_PIN_PER_BANK 32
  23. #define PAD_NUMS_PER_REG 10
  24. #define PAD_BITS_MASK 0x7
  25. #define BITS_PER_PAD 3
  26. #define PIN_REG_OFFSET(x) (((x) / PAD_NUMS_PER_REG) * 4)
  27. #define PIN_BIT_OFFSET(x) (((x) % PAD_NUMS_PER_REG) * BITS_PER_PAD)
  28. #define ARKE_PAD_CTL2A 0xa8
  29. struct ark_pad_ctrl {
  30. int reg;
  31. int offset;
  32. int mask;
  33. };
  34. static struct ark_pad_ctrl ark1668_pin_map[] = {
  35. {0x1e4, 0, 0x3},
  36. {0x1e4, 2, 0x3},
  37. {0x1c0, 0, 0xf},
  38. {0x1c0, 4, 0xf},
  39. {0x1c0, 8, 0xf},
  40. {0x1c0, 12, 0xf},
  41. {0x1c0, 16, 0xf},
  42. {0x1c0, 20, 0xf},
  43. {0x1c0, 24, 0xf},
  44. {0x1c0, 28, 0xf},
  45. {0x1c4, 0, 0xf},
  46. {0x1c4, 4, 0xf},
  47. {0x1c4, 8, 0xf},
  48. {0x1c4, 12, 0xf},
  49. {0x1c4, 16, 0xf},
  50. {0x1c4, 20, 0xf},
  51. {0x1c4, 24, 0xf},
  52. {0x1c4, 28, 0xf},
  53. {0x1c8, 0, 0xf},
  54. {0x1c8, 4, 0xf},
  55. {0x1c8, 8, 0xf},
  56. {0x1c8, 12, 0xf},
  57. {0x1c8, 16, 0xf},
  58. {0x1c8, 20, 0xf},
  59. {0x1c8, 24, 0xf},
  60. {0x1c8, 28, 0xf},
  61. {0x1cc, 0, 0xf},
  62. {0x1cc, 4, 0xf},
  63. {0x1cc, 8, 0xf},
  64. {0x1cc, 12, 0xf},
  65. {0x1dc, 0, 0x3},
  66. {0x1dc, 2, 0x3},
  67. {0x1dc, 4, 0x3},
  68. {0x1dc, 6, 0x3},
  69. {0x1dc, 8, 0x3},
  70. {0x1dc, 10, 0x3},
  71. {0x1dc, 12, 0x3},
  72. {0x1dc, 14, 0x3},
  73. {0x1dc, 16, 0x3},
  74. {0x1d0, 0, 0xf},
  75. {0x1d0, 4, 0xf},
  76. {0x1d0, 8, 0xf},
  77. {0x1d0, 12, 0xf},
  78. {0x1d0, 16, 0xf},
  79. {0x1d0, 20, 0xf},
  80. {0x1d0, 24, 0xf},
  81. {0x1d0, 28, 0xf},
  82. {0x1d4, 0, 0xf},
  83. {0x1d4, 4, 0xf},
  84. {0x1d4, 8, 0xf},
  85. {0x1d4, 12, 0xf},
  86. {0x1d4, 16, 0xf},
  87. {0x1d4, 20, 0xf},
  88. {0x1d4, 24, 0xf},
  89. {0x1d4, 28, 0xf},
  90. {0x1d8, 0, 0xf},
  91. {0x1d8, 4, 0xf},
  92. {0x1d8, 8, 0xf},
  93. {0x1d8, 12, 0xf},
  94. {0x1d8, 16, 0xf},
  95. {0x1d8, 20, 0xf},
  96. {0x1d8, 24, 0xf},
  97. {0x1e0, 0, 0x3},
  98. {0x1e0, 2, 0x3},
  99. {0x1e0, 4, 0x3},
  100. {0x1e0, 6, 0x3},
  101. {0x1e0, 8, 0x3},
  102. {0x1e0, 10, 0x3},
  103. {0x1e0, 12, 0x3},
  104. {0x1e0, 14, 0x3},
  105. {0x1e0, 16, 0x3},
  106. {0x1e0, 18, 0x3},
  107. {0x1e4, 4, 0x3},
  108. {0x1e4, 6, 0x3},
  109. {0x1e4, 8, 0x3},
  110. {0x1e4, 10, 0x3},
  111. {0x1e4, 12, 0x3},
  112. {0x1e4, 14, 0x3},
  113. {0x1e4, 16, 0x3},
  114. {0x1e4, 18, 0x3},
  115. {0x1e4, 20, 0x3},
  116. {0x1e4, 22, 0x3},
  117. {0x1e4, 24, 0x3},
  118. {0x1e4, 26, 0x3},
  119. {0x1e4, 28, 0x3},
  120. {0x1ec, 0, 0x1},
  121. {0x1ec, 1, 0x1},
  122. {0x1ec, 2, 0x1},
  123. {0x1ec, 3, 0x1},
  124. {0x1ec, 4, 0x1},
  125. {0x1ec, 5, 0x1},
  126. {0x1ec, 6, 0x1},
  127. {0x1ec, 7, 0x1},
  128. {0x1ec, 8, 0x1},
  129. {0x1ec, 9, 0x1},
  130. {0x1ec, 10, 0x1},
  131. {0x1ec, 11, 0x1},
  132. {0x1ec, 12, 0x1},
  133. {0x1ec, 13, 0x1},
  134. {0x1ec, 14, 0x1},
  135. {0x1ec, 15, 0x1},
  136. {0x1ec, 16, 0x1},
  137. {0x1ec, 17, 0x1},
  138. {0x1ec, 18, 0x1},
  139. {0x1ec, 19, 0x1},
  140. {0x1ec, 20, 0x1},
  141. {0x1ec, 21, 0x1},
  142. {0x1ec, 22, 0x1},
  143. {0x1ec, 23, 0x1},
  144. {0x1ec, 24, 0x1},
  145. {0x1ec, 25, 0x1},
  146. {0x1ec, 26, 0x1},
  147. {0x1ec, 27, 0x1},
  148. {0x1ec, 28, 0x1},
  149. {0x1ec, 29, 0x1},
  150. {0x1ec, 30, 0x1},
  151. {0x1ec, 31, 0x1},
  152. {0x1f0, 0, 0x1},
  153. {0x1f0, 1, 0x1},
  154. {0x1f0, 2, 0x1},
  155. {0x1f0, 3, 0x1},
  156. {0x1f0, 4, 0x1},
  157. {0x1f0, 5, 0x1},
  158. {0x1f0, 6, 0x1},
  159. {0x1f0, 7, 0x1},
  160. {0x1f0, 8, 0x1},
  161. {0x1f0, 9, 0x1},
  162. {0x1f0, 10, 0x1},
  163. /* pad not mux with gpio */
  164. {0x1e4, 30, 0x1}, /* LVDS CN */
  165. {0x1e4, 31, 0x1}, /* LVDS CP */
  166. {0x1d8, 31, 0x1}, /* I2S1 SADATA IN/OUT */
  167. };
  168. static struct ark_pad_ctrl arkn141_pin_map[] = {
  169. {0x1e8, 4, 0x3},
  170. {0x1e8, 6, 0x3},
  171. {0x1e4, 24, 0x3},
  172. {0x1e4, 26, 0x3},
  173. {0x1c0, 12, 0x7},
  174. {0x1c0, 15, 0x7},
  175. {0x1c0, 18, 0x7},
  176. {0x1c0, 21, 0x7},
  177. {0x1c0, 24, 0x7},
  178. {0x1c0, 27, 0x7},
  179. {0x1c4, 0, 0x7},
  180. {0x1c4, 3, 0x7},
  181. {0x1c4, 6, 0x7},
  182. {0x1c4, 9, 0x7},
  183. {0x1c4, 12, 0x7},
  184. {0x1c4, 15, 0x7},
  185. {0x1c4, 18, 0x7},
  186. {0x1c4, 21, 0x7},
  187. {0x1c4, 24, 0x7},
  188. {0x1c4, 27, 0x7},
  189. {0x1c8, 0, 0x7},
  190. {0x1c8, 3, 0x7},
  191. {0x1c8, 6, 0x7},
  192. {0x1c8, 9, 0x7},
  193. {0x1c8, 12, 0x7},
  194. {0x1c8, 15, 0x7},
  195. {0x1c8, 18, 0x7},
  196. {0x1c8, 21, 0x7},
  197. {0x1c8, 24, 0x7},
  198. {0x1c8, 27, 0x7},
  199. {0x1cc, 0, 0x7},
  200. {0x1cc, 3, 0x7},
  201. {0x1cc, 6, 0x7},
  202. {0x1cc, 9, 0x7},
  203. {0x1cc, 12, 0x7},
  204. {0x1cc, 15, 0x7},
  205. {0x1cc, 18, 0x7},
  206. {0x1cc, 21, 0x7},
  207. {0x1cc, 24, 0x7},
  208. {0x1cc, 27, 0x7},
  209. {0x1d0, 0, 0x7},
  210. {0x1d0, 3, 0x7},
  211. {0x1d0, 6, 0x7},
  212. {0x1d0, 9, 0x7},
  213. {0x1d0, 12, 0x7},
  214. {0x1d0, 15, 0x7},
  215. {0x1d0, 18, 0x7},
  216. {0x1d0, 21, 0x7},
  217. {0x1d0, 24, 0x7},
  218. {0x1d0, 27, 0x7},
  219. {0x1d0, 30, 0x1},
  220. {0x1d0, 31, 0x1},
  221. {0x1d4, 0, 0x7},
  222. {0x1d4, 3, 0x7},
  223. {0x1d4, 6, 0x7},
  224. {0x1d4, 9, 0x7},
  225. {0x1d4, 12, 0x7},
  226. {0x1d4, 15, 0x7},
  227. {0x1d4, 18, 0x7},
  228. {0x1d4, 21, 0x7},
  229. {0x1d4, 24, 0x7},
  230. {0x1d4, 27, 0x7},
  231. {0x1d8, 0, 0x7},
  232. {0x1d8, 3, 0x7},
  233. {0x1d8, 6, 0x7},
  234. {0x1d8, 9, 0x7},
  235. {0x1d8, 12, 0x7},
  236. {0x1d8, 15, 0x7},
  237. {0x1d8, 18, 0x7},
  238. {0x1d8, 21, 0x7},
  239. {0x1d8, 24, 0x7},
  240. {0x1d8, 27, 0x7},
  241. {0x1dc, 0, 0x7},
  242. {0x1dc, 3, 0x7},
  243. {0x1dc, 6, 0x7},
  244. {0x1dc, 9, 0x7},
  245. {0x1dc, 12, 0x7},
  246. {0x1dc, 15, 0x7},
  247. {0x1dc, 18, 0x7},
  248. {0x1dc, 21, 0x7},
  249. {0x1e0, 0, 0x3},
  250. {0x1e0, 2, 0x3},
  251. {0x1e0, 4, 0x3},
  252. {0x1e0, 6, 0x3},
  253. {0x1e0, 8, 0x3},
  254. {0x1e0, 10, 0x3},
  255. {0x1e0, 12, 0x3},
  256. {0x1e0, 14, 0x3},
  257. {0x1e0, 16, 0x3},
  258. {0x1e0, 18, 0x3},
  259. {0x1e0, 20, 0x3},
  260. {0x1e0, 22, 0x3},
  261. {0x1e0, 24, 0x3},
  262. {0x1e0, 26, 0x3},
  263. {0x1e4, 0, 0x1},
  264. {0x1e4, 1, 0x1},
  265. {0x1e4, 2, 0x1},
  266. {0x1e4, 3, 0x1},
  267. {0x1e4, 4, 0x1},
  268. {0x1e4, 5, 0x1},
  269. {0x1e4, 6, 0x1},
  270. {0x1e4, 7, 0x1},
  271. {0x1e4, 8, 0x1},
  272. {0x1e4, 9, 0x1},
  273. {0x1e4, 10, 0x1},
  274. {0x1e4, 11, 0x1},
  275. {0x1e4, 12, 0x1},
  276. {0x1e4, 13, 0x1},
  277. {0x1e4, 16, 0x3},
  278. {0x1e4, 18, 0x3},
  279. {0x1e4, 20, 0x3},
  280. {0x1e4, 22, 0x3},
  281. {0x1e4, 32, 0},
  282. {0x1e4, 32, 0},
  283. {0x1e4, 28, 0x3},
  284. {0x1e4, 30, 0x3},
  285. {0x1e8, 0, 0x3},
  286. {0x1e8, 2, 0x3},
  287. {0x1e8, 32, 0},
  288. {0x1e8, 8, 0x1},
  289. {0x1e8, 9, 0x1},
  290. {0x1e8, 10, 0x1},
  291. {0x1e8, 11, 0x1},
  292. {0x1e8, 32, 0},
  293. {0x1c0, 9, 0x7},
  294. {0x1c0, 6, 0x7},
  295. {0x1c0, 3, 0x7},
  296. {0x1c0, 0, 0x7},
  297. /* pad not mux with gpio */
  298. };
  299. static struct ark_pad_ctrl amt630h_pin_map[] = {
  300. {0xc0, 0, 0x3},
  301. {0xc0, 2, 0x3},
  302. {0xc0, 4, 0x3},
  303. {0xc0, 6, 0x3},
  304. {0xc0, 8, 0x3},
  305. {0xc0, 10, 0x3},
  306. {0xc0, 12, 0x3},
  307. {0xc0, 14, 0x3},
  308. {0xc0, 16, 0x3},
  309. {0xc0, 18, 0x3},
  310. {0xc0, 20, 0x3},
  311. {0xc0, 22, 0x3},
  312. {0xc0, 24, 0x3},
  313. {0xc0, 26, 0x3},
  314. {0xc0, 28, 0x3},
  315. {0xc0, 30, 0x3},
  316. {0xc4, 0, 0x3},
  317. {0xc4, 2, 0x3},
  318. {0xc4, 4, 0x3},
  319. {0xc4, 6, 0x3},
  320. {0xc4, 8, 0x3},
  321. {0xc4, 10, 0x3},
  322. {0xc4, 12, 0x3},
  323. {0xc4, 14, 0x3},
  324. {0xc4, 16, 0x3},
  325. {0xc4, 18, 0x3},
  326. {0xc4, 20, 0x3},
  327. {0xc4, 22, 0x3},
  328. {0xc4, 24, 0x3},
  329. {0xc4, 26, 0x3},
  330. {0xc4, 28, 0x3},
  331. {0xc4, 30, 0x3},
  332. {0xc8, 0, 0x3},
  333. {0xc8, 2, 0x3},
  334. {0xc8, 4, 0x3},
  335. {0xc8, 6, 0x3},
  336. {0xc8, 8, 0x3},
  337. {0xc8, 10, 0x3},
  338. {0xc8, 12, 0x3},
  339. {0xc8, 14, 0x3},
  340. {0xc8, 16, 0x3},
  341. {0xc8, 18, 0x3},
  342. {0xc8, 20, 0x3},
  343. {0xc8, 22, 0x3},
  344. {0xc8, 24, 0x3},
  345. {0xc8, 26, 0x3},
  346. {0xc8, 28, 0x3},
  347. {0xc8, 30, 0x3},
  348. {0xcc, 0, 0x3},
  349. {0xcc, 2, 0x3},
  350. {0xcc, 4, 0x3},
  351. {0xcc, 6, 0x3},
  352. {0xcc, 8, 0x3},
  353. {0xcc, 10, 0x3},
  354. {0xcc, 12, 0x3},
  355. {0xcc, 14, 0x3},
  356. {0xcc, 16, 0x3},
  357. {0xcc, 18, 0x3},
  358. {0xcc, 20, 0x3},
  359. {0xcc, 22, 0x3},
  360. {0xcc, 24, 0x3},
  361. {0xcc, 26, 0x3},
  362. {0xcc, 28, 0x3},
  363. {0xcc, 30, 0x3},
  364. {0xd0, 0, 0x3},
  365. {0xd0, 2, 0x3},
  366. {0xd0, 4, 0x3},
  367. {0xd0, 6, 0x3},
  368. {0xd0, 8, 0x3},
  369. {0xd0, 10, 0x3},
  370. {0xd0, 12, 0x3},
  371. {0xd0, 14, 0x3},
  372. {0xd0, 16, 0x3},
  373. {0xd0, 18, 0x3},
  374. {0xd0, 20, 0x3},
  375. {0xd0, 22, 0x3},
  376. {0xd0, 24, 0x3},
  377. {0xd0, 26, 0x3},
  378. {0xd0, 28, 0x3},
  379. {0xd0, 30, 0x3},
  380. {0xd4, 0, 0x3},
  381. {0xd4, 2, 0x3},
  382. {0xd4, 4, 0x3},
  383. {0xd4, 6, 0x3},
  384. {0xd4, 8, 0x3},
  385. {0xd4, 10, 0x3},
  386. {0xd4, 12, 0x3},
  387. {0xd4, 14, 0x3},
  388. {0xd4, 16, 0x3},
  389. {0xd4, 18, 0x3},
  390. {0xd4, 20, 0x3},
  391. {0xd4, 22, 0x3},
  392. {0xd4, 24, 0x3},
  393. {0xd4, 26, 0x3},
  394. {0xd4, 28, 0x3},
  395. {0xd4, 30, 0x3},
  396. {0xd8, 0, 0x3},
  397. {0xd8, 2, 0x3},
  398. {0xd8, 4, 0x3},
  399. {0xd8, 6, 0x3},
  400. {0xd8, 8, 0x3},
  401. {0xd8, 10, 0x3},
  402. /* pad not mux with gpio */
  403. };
  404. struct ark_pmx_func {
  405. const char *name;
  406. const char **groups;
  407. unsigned ngroups;
  408. };
  409. struct ark_pmx_pin {
  410. uint32_t bank;
  411. uint32_t pin;
  412. uint32_t val;
  413. unsigned long conf;
  414. };
  415. struct ark_group_mux {
  416. uint32_t group_mux_reg;
  417. uint32_t group_mux_offset;
  418. uint32_t group_mux_mask;
  419. uint32_t group_mux_val;
  420. };
  421. struct ark_pin_group {
  422. const char *name;
  423. struct ark_pmx_pin *pins_conf;
  424. unsigned int *pins;
  425. unsigned npins;
  426. struct ark_group_mux *group_muxs;
  427. unsigned ngpmuxs;
  428. };
  429. struct ark_pinctrl {
  430. struct device *dev;
  431. struct pinctrl_dev *ctldev;
  432. void __iomem *regbase;
  433. struct ark_pad_ctrl *pctrl;
  434. int npins;
  435. int gpio_mux_pins;
  436. int is_arke;
  437. u32 pad_reg_offset;
  438. struct ark_pmx_func *functions;
  439. int nfunctions;
  440. struct ark_pin_group *groups;
  441. int ngroups;
  442. };
  443. static inline const struct ark_pin_group *ark_pinctrl_find_group_by_name(const struct ark_pinctrl *info,
  444. const char *name)
  445. {
  446. const struct ark_pin_group *grp = NULL;
  447. int i;
  448. for (i = 0; i < info->ngroups; i++) {
  449. if (strcmp(info->groups[i].name, name))
  450. continue;
  451. grp = &info->groups[i];
  452. dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins, grp->pins[0]);
  453. break;
  454. }
  455. return grp;
  456. }
  457. static int ark_get_groups_count(struct pinctrl_dev *pctldev)
  458. {
  459. struct ark_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  460. return info->ngroups;
  461. }
  462. static const char *ark_get_group_name(struct pinctrl_dev *pctldev, unsigned selector)
  463. {
  464. struct ark_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  465. return info->groups[selector].name;
  466. }
  467. static int ark_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector, const unsigned **pins, unsigned *npins)
  468. {
  469. struct ark_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  470. if (selector >= info->ngroups)
  471. return -EINVAL;
  472. *pins = info->groups[selector].pins;
  473. *npins = info->groups[selector].npins;
  474. return 0;
  475. }
  476. static void ark_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, unsigned offset)
  477. {
  478. seq_printf(s, "%s", dev_name(pctldev->dev));
  479. }
  480. static int ark_dt_node_to_map(struct pinctrl_dev *pctldev,
  481. struct device_node *np, struct pinctrl_map **map, unsigned *num_maps)
  482. {
  483. struct ark_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  484. const struct ark_pin_group *grp;
  485. struct pinctrl_map *new_map;
  486. struct device_node *parent;
  487. int map_num = 1;
  488. int i;
  489. /*
  490. * first find the group of this node and check if we need to create
  491. * config maps for pins
  492. */
  493. grp = ark_pinctrl_find_group_by_name(info, np->name);
  494. if (!grp) {
  495. dev_err(info->dev, "unable to find group for node %s\n", np->name);
  496. return -EINVAL;
  497. }
  498. map_num += grp->npins;
  499. new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num, GFP_KERNEL);
  500. if (!new_map)
  501. return -ENOMEM;
  502. *map = new_map;
  503. *num_maps = map_num;
  504. /* create mux map */
  505. parent = of_get_parent(np);
  506. if (!parent) {
  507. devm_kfree(pctldev->dev, new_map);
  508. return -EINVAL;
  509. }
  510. new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
  511. new_map[0].data.mux.function = parent->name;
  512. new_map[0].data.mux.group = np->name;
  513. of_node_put(parent);
  514. /* create config map */
  515. new_map++;
  516. for (i = 0; i < grp->npins; i++) {
  517. new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
  518. new_map[i].data.configs.group_or_pin = pin_get_name(pctldev, grp->pins[i]);
  519. new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
  520. new_map[i].data.configs.num_configs = 1;
  521. }
  522. dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
  523. (*map)->data.mux.function, (*map)->data.mux.group, map_num);
  524. return 0;
  525. }
  526. static void ark_dt_free_map(struct pinctrl_dev *pctldev, struct pinctrl_map *map, unsigned num_maps)
  527. {
  528. }
  529. static const struct pinctrl_ops ark_pctrl_ops = {
  530. .get_groups_count = ark_get_groups_count,
  531. .get_group_name = ark_get_group_name,
  532. .get_group_pins = ark_get_group_pins,
  533. .pin_dbg_show = ark_pin_dbg_show,
  534. .dt_node_to_map = ark_dt_node_to_map,
  535. .dt_free_map = ark_dt_free_map,
  536. };
  537. static int ark_pmx_set(struct pinctrl_dev *pctldev, unsigned selector, unsigned group)
  538. {
  539. struct ark_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  540. const struct ark_pin_group *grp = &info->groups[group];
  541. const struct ark_pmx_pin *pins_conf = grp->pins_conf;
  542. const struct ark_pmx_pin *pin;
  543. uint32_t npins = info->groups[group].npins;
  544. int i;
  545. struct ark_pad_ctrl *pctrl;
  546. uint32_t val;
  547. dev_dbg(info->dev, "enable function %s group %s\n", info->functions[selector].name, info->groups[group].name);
  548. for (i = 0; i < npins; i++) {
  549. pin = &pins_conf[i];
  550. if (info->is_arke) {
  551. u32 npin = info->groups[group].pins[i];
  552. val = readl_relaxed(info->regbase + info->pad_reg_offset + PIN_REG_OFFSET(npin));
  553. val &= ~(PAD_BITS_MASK << PIN_BIT_OFFSET(npin));
  554. val |= pin->val << PIN_BIT_OFFSET(npin);
  555. writel_relaxed(val, info->regbase + info->pad_reg_offset + PIN_REG_OFFSET(npin));
  556. } else {
  557. pctrl = &info->pctrl[info->groups[group].pins[i]];
  558. val = readl_relaxed(info->regbase + pctrl->reg);
  559. val &= ~(pctrl->mask << pctrl->offset);
  560. val |= pin->val << pctrl->offset;
  561. writel_relaxed(val, info->regbase + pctrl->reg);
  562. }
  563. }
  564. for (i = 0; i < grp->ngpmuxs; i++) {
  565. val = readl_relaxed(info->regbase + grp->group_muxs[i].group_mux_reg);
  566. val &= ~(grp->group_muxs[i].group_mux_mask << grp->group_muxs[i].group_mux_offset);
  567. val |= grp->group_muxs[i].group_mux_val << grp->group_muxs[i].group_mux_offset;
  568. writel_relaxed(val, info->regbase + grp->group_muxs[i].group_mux_reg);
  569. }
  570. return 0;
  571. }
  572. static int ark_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  573. {
  574. struct ark_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  575. return info->nfunctions;
  576. }
  577. static const char *ark_pmx_get_func_name(struct pinctrl_dev *pctldev, unsigned selector)
  578. {
  579. struct ark_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  580. return info->functions[selector].name;
  581. }
  582. static int ark_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
  583. const char *const **groups, unsigned *const num_groups)
  584. {
  585. struct ark_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  586. *groups = info->functions[selector].groups;
  587. *num_groups = info->functions[selector].ngroups;
  588. return 0;
  589. }
  590. static int ark_gpio_request_enable(struct pinctrl_dev *pctldev, struct pinctrl_gpio_range *range, unsigned offset)
  591. {
  592. struct ark_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  593. struct ark_pad_ctrl *pctrl;
  594. u32 val;
  595. if (offset >= info->npins)
  596. return -EINVAL;
  597. dev_dbg(info->dev, "enable pin %u as GPIO\n", offset);
  598. if (info->is_arke) {
  599. if (offset < info->gpio_mux_pins) {
  600. val = readl_relaxed(info->regbase + info->pad_reg_offset + PIN_REG_OFFSET(offset));
  601. val &= ~(PAD_BITS_MASK << PIN_BIT_OFFSET(offset));
  602. writel_relaxed(val, info->regbase + info->pad_reg_offset + PIN_REG_OFFSET(offset));
  603. } else {
  604. if (offset >= 174 && offset <= 181) {
  605. val = readl_relaxed(info->regbase + ARKE_PAD_CTL2A);
  606. val &= ~(0x3 << 30);
  607. writel_relaxed(val, info->regbase + ARKE_PAD_CTL2A);
  608. }
  609. }
  610. } else {
  611. pctrl = &info->pctrl[offset];
  612. val = readl_relaxed(info->regbase + pctrl->reg);
  613. val &= ~(pctrl->mask << pctrl->offset);
  614. writel_relaxed(val, info->regbase + pctrl->reg);
  615. }
  616. return 0;
  617. }
  618. static void ark_gpio_disable_free(struct pinctrl_dev *pctldev, struct pinctrl_gpio_range *range, unsigned offset)
  619. {
  620. struct ark_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  621. dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
  622. /* Set the pin to some default state, GPIO is usually default */
  623. }
  624. static const struct pinmux_ops ark_pmx_ops = {
  625. .get_functions_count = ark_pmx_get_funcs_count,
  626. .get_function_name = ark_pmx_get_func_name,
  627. .get_function_groups = ark_pmx_get_groups,
  628. .set_mux = ark_pmx_set,
  629. .gpio_request_enable = ark_gpio_request_enable,
  630. .gpio_disable_free = ark_gpio_disable_free,
  631. };
  632. static int ark_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin_id, unsigned long *config)
  633. {
  634. return 0;
  635. }
  636. static int ark_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin_id, unsigned long *configs, unsigned num_configs)
  637. {
  638. return 0;
  639. }
  640. static void ark_pinconf_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, unsigned pin_id)
  641. {
  642. }
  643. static void ark_pinconf_group_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s, unsigned group)
  644. {
  645. }
  646. static const struct pinconf_ops ark_pinconf_ops = {
  647. .pin_config_get = ark_pinconf_get,
  648. .pin_config_set = ark_pinconf_set,
  649. .pin_config_dbg_show = ark_pinconf_dbg_show,
  650. .pin_config_group_dbg_show = ark_pinconf_group_dbg_show,
  651. };
  652. static struct pinctrl_desc ark_pinctrl_desc = {
  653. .pctlops = &ark_pctrl_ops,
  654. .pmxops = &ark_pmx_ops,
  655. .confops = &ark_pinconf_ops,
  656. .owner = THIS_MODULE,
  657. };
  658. static void ark_pinctrl_child_count(struct ark_pinctrl *info, struct device_node *np)
  659. {
  660. struct device_node *child;
  661. for_each_child_of_node(np, child) {
  662. info->nfunctions++;
  663. info->ngroups += of_get_child_count(child);
  664. }
  665. }
  666. static int ark_pinctrl_parse_groups(struct device_node *np,
  667. struct ark_pin_group *grp, struct ark_pinctrl *info, u32 index)
  668. {
  669. struct ark_pmx_pin *pin;
  670. int size;
  671. const __be32 *list;
  672. int i;
  673. dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
  674. /* Initialise group */
  675. grp->name = np->name;
  676. /*
  677. * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
  678. * do sanity check and calculate pins number
  679. */
  680. list = of_get_property(np, "ark,pins", &size);
  681. if (!list)
  682. return -EINVAL;
  683. /* we do not check return since it's safe node passed down */
  684. size /= sizeof(*list);
  685. if (!size || size % 3) {
  686. dev_err(info->dev, "wrong pins number or pins and configs should be by 3\n");
  687. return -EINVAL;
  688. }
  689. grp->npins = size / 3;
  690. pin = grp->pins_conf = devm_kzalloc(info->dev, grp->npins * sizeof(struct ark_pmx_pin), GFP_KERNEL);
  691. grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int), GFP_KERNEL);
  692. if (!grp->pins_conf || !grp->pins)
  693. return -ENOMEM;
  694. for (i = 0; i < grp->npins; i++) {
  695. pin->bank = be32_to_cpu(*list++);
  696. pin->pin = be32_to_cpu(*list++);
  697. grp->pins[i] = pin->bank * MAX_PIN_PER_BANK + pin->pin;
  698. pin->val = be32_to_cpu(*list++);
  699. dev_dbg(info->dev, "pin%d val=%d.\n", grp->pins[i], pin->val);
  700. pin++;
  701. }
  702. list = of_get_property(np, "group-mux", &size);
  703. if (list) {
  704. size /= sizeof(*list);
  705. if (size % 4) {
  706. dev_err(info->dev, "wrong groupmux number or groupmuxs and configs should be by 4\n");
  707. return -EINVAL;
  708. }
  709. grp->ngpmuxs = size / 4;
  710. grp->group_muxs = devm_kzalloc(info->dev, grp->ngpmuxs * sizeof(struct ark_group_mux), GFP_KERNEL);
  711. if (!grp->group_muxs)
  712. return -ENOMEM;
  713. for (i = 0; i < grp->ngpmuxs; i++) {
  714. grp->group_muxs[i].group_mux_reg = be32_to_cpu(*list++);
  715. grp->group_muxs[i].group_mux_offset = be32_to_cpu(*list++);
  716. grp->group_muxs[i].group_mux_mask = be32_to_cpu(*list++);
  717. grp->group_muxs[i].group_mux_val = be32_to_cpu(*list++);
  718. }
  719. }
  720. return 0;
  721. }
  722. static int ark_pinctrl_parse_functions(struct device_node *np, struct ark_pinctrl *info, u32 index)
  723. {
  724. struct device_node *child;
  725. struct ark_pmx_func *func;
  726. struct ark_pin_group *grp;
  727. int ret;
  728. static u32 grp_index;
  729. u32 i = 0;
  730. dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
  731. func = &info->functions[index];
  732. /* Initialise function */
  733. func->name = np->name;
  734. func->ngroups = of_get_child_count(np);
  735. if (func->ngroups == 0) {
  736. dev_err(info->dev, "no groups defined\n");
  737. return -EINVAL;
  738. }
  739. func->groups = devm_kzalloc(info->dev, func->ngroups * sizeof(char *), GFP_KERNEL);
  740. if (!func->groups)
  741. return -ENOMEM;
  742. for_each_child_of_node(np, child) {
  743. func->groups[i] = child->name;
  744. grp = &info->groups[grp_index++];
  745. ret = ark_pinctrl_parse_groups(child, grp, info, i++);
  746. if (ret) {
  747. of_node_put(child);
  748. return ret;
  749. }
  750. }
  751. return 0;
  752. }
  753. static const struct of_device_id ark_pinctrl_of_match[] = {
  754. {.compatible = "arkmicro,ark1668-pinctrl",},
  755. {.compatible = "arkmicro,arkn141-pinctrl",},
  756. {.compatible = "arkmicro,arke-pinctrl",},
  757. {.compatible = "arkmicro,amt630h-pinctrl",},
  758. { /* sentinel */ }
  759. };
  760. static int ark_pinctrl_probe_dt(struct platform_device *pdev, struct ark_pinctrl *info)
  761. {
  762. int ret = 0;
  763. struct device_node *np = pdev->dev.of_node;
  764. struct device_node *child;
  765. int i = 0;
  766. if (!np)
  767. return -ENODEV;
  768. info->dev = &pdev->dev;
  769. ark_pinctrl_child_count(info, np);
  770. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  771. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  772. info->functions = devm_kzalloc(&pdev->dev, info->nfunctions * sizeof(struct ark_pmx_func), GFP_KERNEL);
  773. if (!info->functions)
  774. return -ENOMEM;
  775. info->groups = devm_kzalloc(&pdev->dev, info->ngroups * sizeof(struct ark_pin_group), GFP_KERNEL);
  776. if (!info->groups)
  777. return -ENOMEM;
  778. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  779. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  780. for_each_child_of_node(np, child) {
  781. ret = ark_pinctrl_parse_functions(child, info, i++);
  782. if (ret) {
  783. dev_err(&pdev->dev, "failed to parse function\n");
  784. of_node_put(child);
  785. return ret;
  786. }
  787. }
  788. return 0;
  789. }
  790. static int ark_pinctrl_probe(struct platform_device *pdev)
  791. {
  792. struct ark_pinctrl *info;
  793. struct pinctrl_pin_desc *pdesc;
  794. struct resource *res;
  795. int ret, i;
  796. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  797. if (!info)
  798. return -ENOMEM;
  799. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  800. info->regbase = devm_ioremap_resource(&pdev->dev, res);
  801. if (IS_ERR(info->regbase)) {
  802. return PTR_ERR(info->regbase);
  803. }
  804. ret = ark_pinctrl_probe_dt(pdev, info);
  805. if (ret)
  806. return ret;
  807. ark_pinctrl_desc.name = dev_name(&pdev->dev);
  808. if (of_device_is_compatible(pdev->dev.of_node, "arkmicro,ark1668-pinctrl")) {
  809. info->pctrl = ark1668_pin_map;
  810. info->npins = ark_pinctrl_desc.npins = ARRAY_SIZE(ark1668_pin_map);
  811. } else if (of_device_is_compatible(pdev->dev.of_node, "arkmicro,arkn141-pinctrl")) {
  812. info->pctrl = arkn141_pin_map;
  813. info->npins = ark_pinctrl_desc.npins = ARRAY_SIZE(arkn141_pin_map);
  814. } else if (of_device_is_compatible(pdev->dev.of_node, "arkmicro,amt630h-pinctrl")) {
  815. info->pctrl = amt630h_pin_map;
  816. info->npins = ark_pinctrl_desc.npins = ARRAY_SIZE(amt630h_pin_map);
  817. } else if (of_device_is_compatible(pdev->dev.of_node, "arkmicro,arke-pinctrl")) {
  818. info->is_arke = 1;
  819. if (of_property_read_u32(pdev->dev.of_node, "npins", &info->npins)) {
  820. dev_err(&pdev->dev, "could not get npins.\n");
  821. return -EINVAL;
  822. }
  823. ark_pinctrl_desc.npins = info->npins;
  824. if (of_property_read_u32(pdev->dev.of_node, "gpio-mux-pins", &info->gpio_mux_pins)) {
  825. dev_err(&pdev->dev, "could not get gpio_mux_pins.\n");
  826. return -EINVAL;
  827. }
  828. if (of_property_read_u32(pdev->dev.of_node, "pad-reg-offset", &info->pad_reg_offset)) {
  829. dev_err(&pdev->dev, "could not get pad_reg_offset.\n");
  830. return -EINVAL;
  831. }
  832. }
  833. ark_pinctrl_desc.pins = pdesc = devm_kzalloc(&pdev->dev, sizeof(*pdesc) * ark_pinctrl_desc.npins, GFP_KERNEL);
  834. if (!ark_pinctrl_desc.pins)
  835. return -ENOMEM;
  836. for (i = 0; i < ark_pinctrl_desc.npins; i++) {
  837. pdesc->number = i;
  838. pdesc->name = kasprintf(GFP_KERNEL, "pin%d", i);
  839. pdesc++;
  840. }
  841. platform_set_drvdata(pdev, info);
  842. info->ctldev = devm_pinctrl_register(&pdev->dev, &ark_pinctrl_desc, info);
  843. if (IS_ERR(info->ctldev)) {
  844. dev_err(&pdev->dev, "could not register ark pinctrl driver\n");
  845. return PTR_ERR(info->ctldev);
  846. }
  847. dev_info(&pdev->dev, "initialized ark pinctrl driver\n");
  848. return 0;
  849. }
  850. static struct platform_driver ark_pinctrl_driver = {
  851. .driver = {
  852. .name = "pinctrl-ark",
  853. .of_match_table = of_match_ptr(ark_pinctrl_of_match),
  854. },
  855. .probe = ark_pinctrl_probe,
  856. };
  857. static int __init ark_pinctrl_init(void)
  858. {
  859. return platform_driver_register(&ark_pinctrl_driver);
  860. }
  861. postcore_initcall(ark_pinctrl_init);
  862. MODULE_AUTHOR("Sim");
  863. MODULE_DESCRIPTION("Arkmicro Pinctrl driver");
  864. MODULE_LICENSE("GPL v2");