pinctrl-ark.c 23 KB

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