pinctrl-ingenic.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. /*
  2. * Ingenic SoCs pinctrl driver
  3. *
  4. * Copyright (c) 2017 Paul Cercueil <paul@crapouillou.net>
  5. *
  6. * License terms: GNU General Public License (GPL) version 2
  7. */
  8. #include <linux/compiler.h>
  9. #include <linux/gpio/driver.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/of_device.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/pinctrl/pinctrl.h>
  16. #include <linux/pinctrl/pinmux.h>
  17. #include <linux/pinctrl/pinconf.h>
  18. #include <linux/pinctrl/pinconf-generic.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include "core.h"
  23. #include "pinconf.h"
  24. #include "pinmux.h"
  25. #define GPIO_PIN 0x00
  26. #define GPIO_MSK 0x20
  27. #define JZ4740_GPIO_DATA 0x10
  28. #define JZ4740_GPIO_PULL_DIS 0x30
  29. #define JZ4740_GPIO_FUNC 0x40
  30. #define JZ4740_GPIO_SELECT 0x50
  31. #define JZ4740_GPIO_DIR 0x60
  32. #define JZ4740_GPIO_TRIG 0x70
  33. #define JZ4740_GPIO_FLAG 0x80
  34. #define JZ4770_GPIO_INT 0x10
  35. #define JZ4770_GPIO_PAT1 0x30
  36. #define JZ4770_GPIO_PAT0 0x40
  37. #define JZ4770_GPIO_FLAG 0x50
  38. #define JZ4770_GPIO_PEN 0x70
  39. #define REG_SET(x) ((x) + 0x4)
  40. #define REG_CLEAR(x) ((x) + 0x8)
  41. #define PINS_PER_GPIO_CHIP 32
  42. enum jz_version {
  43. ID_JZ4740,
  44. ID_JZ4725B,
  45. ID_JZ4770,
  46. ID_JZ4780,
  47. };
  48. struct ingenic_chip_info {
  49. unsigned int num_chips;
  50. const struct group_desc *groups;
  51. unsigned int num_groups;
  52. const struct function_desc *functions;
  53. unsigned int num_functions;
  54. const u32 *pull_ups, *pull_downs;
  55. };
  56. struct ingenic_pinctrl {
  57. struct device *dev;
  58. struct regmap *map;
  59. struct pinctrl_dev *pctl;
  60. struct pinctrl_pin_desc *pdesc;
  61. enum jz_version version;
  62. const struct ingenic_chip_info *info;
  63. };
  64. struct ingenic_gpio_chip {
  65. struct ingenic_pinctrl *jzpc;
  66. struct gpio_chip gc;
  67. struct irq_chip irq_chip;
  68. unsigned int irq, reg_base;
  69. };
  70. static const u32 jz4740_pull_ups[4] = {
  71. 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
  72. };
  73. static const u32 jz4740_pull_downs[4] = {
  74. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  75. };
  76. static int jz4740_mmc_1bit_pins[] = { 0x69, 0x68, 0x6a, };
  77. static int jz4740_mmc_4bit_pins[] = { 0x6b, 0x6c, 0x6d, };
  78. static int jz4740_uart0_data_pins[] = { 0x7a, 0x79, };
  79. static int jz4740_uart0_hwflow_pins[] = { 0x7e, 0x7f, };
  80. static int jz4740_uart1_data_pins[] = { 0x7e, 0x7f, };
  81. static int jz4740_lcd_8bit_pins[] = {
  82. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x52, 0x53, 0x54,
  83. };
  84. static int jz4740_lcd_16bit_pins[] = {
  85. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x55,
  86. };
  87. static int jz4740_lcd_18bit_pins[] = { 0x50, 0x51, };
  88. static int jz4740_lcd_18bit_tft_pins[] = { 0x56, 0x57, 0x31, 0x32, };
  89. static int jz4740_nand_cs1_pins[] = { 0x39, };
  90. static int jz4740_nand_cs2_pins[] = { 0x3a, };
  91. static int jz4740_nand_cs3_pins[] = { 0x3b, };
  92. static int jz4740_nand_cs4_pins[] = { 0x3c, };
  93. static int jz4740_pwm_pwm0_pins[] = { 0x77, };
  94. static int jz4740_pwm_pwm1_pins[] = { 0x78, };
  95. static int jz4740_pwm_pwm2_pins[] = { 0x79, };
  96. static int jz4740_pwm_pwm3_pins[] = { 0x7a, };
  97. static int jz4740_pwm_pwm4_pins[] = { 0x7b, };
  98. static int jz4740_pwm_pwm5_pins[] = { 0x7c, };
  99. static int jz4740_pwm_pwm6_pins[] = { 0x7e, };
  100. static int jz4740_pwm_pwm7_pins[] = { 0x7f, };
  101. static int jz4740_mmc_1bit_funcs[] = { 0, 0, 0, };
  102. static int jz4740_mmc_4bit_funcs[] = { 0, 0, 0, };
  103. static int jz4740_uart0_data_funcs[] = { 1, 1, };
  104. static int jz4740_uart0_hwflow_funcs[] = { 1, 1, };
  105. static int jz4740_uart1_data_funcs[] = { 2, 2, };
  106. static int jz4740_lcd_8bit_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
  107. static int jz4740_lcd_16bit_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, };
  108. static int jz4740_lcd_18bit_funcs[] = { 0, 0, };
  109. static int jz4740_lcd_18bit_tft_funcs[] = { 0, 0, 0, 0, };
  110. static int jz4740_nand_cs1_funcs[] = { 0, };
  111. static int jz4740_nand_cs2_funcs[] = { 0, };
  112. static int jz4740_nand_cs3_funcs[] = { 0, };
  113. static int jz4740_nand_cs4_funcs[] = { 0, };
  114. static int jz4740_pwm_pwm0_funcs[] = { 0, };
  115. static int jz4740_pwm_pwm1_funcs[] = { 0, };
  116. static int jz4740_pwm_pwm2_funcs[] = { 0, };
  117. static int jz4740_pwm_pwm3_funcs[] = { 0, };
  118. static int jz4740_pwm_pwm4_funcs[] = { 0, };
  119. static int jz4740_pwm_pwm5_funcs[] = { 0, };
  120. static int jz4740_pwm_pwm6_funcs[] = { 0, };
  121. static int jz4740_pwm_pwm7_funcs[] = { 0, };
  122. #define INGENIC_PIN_GROUP(name, id) \
  123. { \
  124. name, \
  125. id##_pins, \
  126. ARRAY_SIZE(id##_pins), \
  127. id##_funcs, \
  128. }
  129. static const struct group_desc jz4740_groups[] = {
  130. INGENIC_PIN_GROUP("mmc-1bit", jz4740_mmc_1bit),
  131. INGENIC_PIN_GROUP("mmc-4bit", jz4740_mmc_4bit),
  132. INGENIC_PIN_GROUP("uart0-data", jz4740_uart0_data),
  133. INGENIC_PIN_GROUP("uart0-hwflow", jz4740_uart0_hwflow),
  134. INGENIC_PIN_GROUP("uart1-data", jz4740_uart1_data),
  135. INGENIC_PIN_GROUP("lcd-8bit", jz4740_lcd_8bit),
  136. INGENIC_PIN_GROUP("lcd-16bit", jz4740_lcd_16bit),
  137. INGENIC_PIN_GROUP("lcd-18bit", jz4740_lcd_18bit),
  138. INGENIC_PIN_GROUP("lcd-18bit-tft", jz4740_lcd_18bit_tft),
  139. { "lcd-no-pins", },
  140. INGENIC_PIN_GROUP("nand-cs1", jz4740_nand_cs1),
  141. INGENIC_PIN_GROUP("nand-cs2", jz4740_nand_cs2),
  142. INGENIC_PIN_GROUP("nand-cs3", jz4740_nand_cs3),
  143. INGENIC_PIN_GROUP("nand-cs4", jz4740_nand_cs4),
  144. INGENIC_PIN_GROUP("pwm0", jz4740_pwm_pwm0),
  145. INGENIC_PIN_GROUP("pwm1", jz4740_pwm_pwm1),
  146. INGENIC_PIN_GROUP("pwm2", jz4740_pwm_pwm2),
  147. INGENIC_PIN_GROUP("pwm3", jz4740_pwm_pwm3),
  148. INGENIC_PIN_GROUP("pwm4", jz4740_pwm_pwm4),
  149. INGENIC_PIN_GROUP("pwm5", jz4740_pwm_pwm5),
  150. INGENIC_PIN_GROUP("pwm6", jz4740_pwm_pwm6),
  151. INGENIC_PIN_GROUP("pwm7", jz4740_pwm_pwm7),
  152. };
  153. static const char *jz4740_mmc_groups[] = { "mmc-1bit", "mmc-4bit", };
  154. static const char *jz4740_uart0_groups[] = { "uart0-data", "uart0-hwflow", };
  155. static const char *jz4740_uart1_groups[] = { "uart1-data", };
  156. static const char *jz4740_lcd_groups[] = {
  157. "lcd-8bit", "lcd-16bit", "lcd-18bit", "lcd-18bit-tft", "lcd-no-pins",
  158. };
  159. static const char *jz4740_nand_groups[] = {
  160. "nand-cs1", "nand-cs2", "nand-cs3", "nand-cs4",
  161. };
  162. static const char *jz4740_pwm0_groups[] = { "pwm0", };
  163. static const char *jz4740_pwm1_groups[] = { "pwm1", };
  164. static const char *jz4740_pwm2_groups[] = { "pwm2", };
  165. static const char *jz4740_pwm3_groups[] = { "pwm3", };
  166. static const char *jz4740_pwm4_groups[] = { "pwm4", };
  167. static const char *jz4740_pwm5_groups[] = { "pwm5", };
  168. static const char *jz4740_pwm6_groups[] = { "pwm6", };
  169. static const char *jz4740_pwm7_groups[] = { "pwm7", };
  170. static const struct function_desc jz4740_functions[] = {
  171. { "mmc", jz4740_mmc_groups, ARRAY_SIZE(jz4740_mmc_groups), },
  172. { "uart0", jz4740_uart0_groups, ARRAY_SIZE(jz4740_uart0_groups), },
  173. { "uart1", jz4740_uart1_groups, ARRAY_SIZE(jz4740_uart1_groups), },
  174. { "lcd", jz4740_lcd_groups, ARRAY_SIZE(jz4740_lcd_groups), },
  175. { "nand", jz4740_nand_groups, ARRAY_SIZE(jz4740_nand_groups), },
  176. { "pwm0", jz4740_pwm0_groups, ARRAY_SIZE(jz4740_pwm0_groups), },
  177. { "pwm1", jz4740_pwm1_groups, ARRAY_SIZE(jz4740_pwm1_groups), },
  178. { "pwm2", jz4740_pwm2_groups, ARRAY_SIZE(jz4740_pwm2_groups), },
  179. { "pwm3", jz4740_pwm3_groups, ARRAY_SIZE(jz4740_pwm3_groups), },
  180. { "pwm4", jz4740_pwm4_groups, ARRAY_SIZE(jz4740_pwm4_groups), },
  181. { "pwm5", jz4740_pwm5_groups, ARRAY_SIZE(jz4740_pwm5_groups), },
  182. { "pwm6", jz4740_pwm6_groups, ARRAY_SIZE(jz4740_pwm6_groups), },
  183. { "pwm7", jz4740_pwm7_groups, ARRAY_SIZE(jz4740_pwm7_groups), },
  184. };
  185. static const struct ingenic_chip_info jz4740_chip_info = {
  186. .num_chips = 4,
  187. .groups = jz4740_groups,
  188. .num_groups = ARRAY_SIZE(jz4740_groups),
  189. .functions = jz4740_functions,
  190. .num_functions = ARRAY_SIZE(jz4740_functions),
  191. .pull_ups = jz4740_pull_ups,
  192. .pull_downs = jz4740_pull_downs,
  193. };
  194. static int jz4725b_mmc0_1bit_pins[] = { 0x48, 0x49, 0x5c, };
  195. static int jz4725b_mmc0_4bit_pins[] = { 0x5d, 0x5b, 0x56, };
  196. static int jz4725b_mmc1_1bit_pins[] = { 0x7a, 0x7b, 0x7c, };
  197. static int jz4725b_mmc1_4bit_pins[] = { 0x7d, 0x7e, 0x7f, };
  198. static int jz4725b_uart_data_pins[] = { 0x4c, 0x4d, };
  199. static int jz4725b_nand_cs1_pins[] = { 0x55, };
  200. static int jz4725b_nand_cs2_pins[] = { 0x56, };
  201. static int jz4725b_nand_cs3_pins[] = { 0x57, };
  202. static int jz4725b_nand_cs4_pins[] = { 0x58, };
  203. static int jz4725b_nand_cle_ale_pins[] = { 0x48, 0x49 };
  204. static int jz4725b_nand_fre_fwe_pins[] = { 0x5c, 0x5d };
  205. static int jz4725b_pwm_pwm0_pins[] = { 0x4a, };
  206. static int jz4725b_pwm_pwm1_pins[] = { 0x4b, };
  207. static int jz4725b_pwm_pwm2_pins[] = { 0x4c, };
  208. static int jz4725b_pwm_pwm3_pins[] = { 0x4d, };
  209. static int jz4725b_pwm_pwm4_pins[] = { 0x4e, };
  210. static int jz4725b_pwm_pwm5_pins[] = { 0x4f, };
  211. static int jz4725b_mmc0_1bit_funcs[] = { 1, 1, 1, };
  212. static int jz4725b_mmc0_4bit_funcs[] = { 1, 0, 1, };
  213. static int jz4725b_mmc1_1bit_funcs[] = { 0, 0, 0, };
  214. static int jz4725b_mmc1_4bit_funcs[] = { 0, 0, 0, };
  215. static int jz4725b_uart_data_funcs[] = { 1, 1, };
  216. static int jz4725b_nand_cs1_funcs[] = { 0, };
  217. static int jz4725b_nand_cs2_funcs[] = { 0, };
  218. static int jz4725b_nand_cs3_funcs[] = { 0, };
  219. static int jz4725b_nand_cs4_funcs[] = { 0, };
  220. static int jz4725b_nand_cle_ale_funcs[] = { 0, 0, };
  221. static int jz4725b_nand_fre_fwe_funcs[] = { 0, 0, };
  222. static int jz4725b_pwm_pwm0_funcs[] = { 0, };
  223. static int jz4725b_pwm_pwm1_funcs[] = { 0, };
  224. static int jz4725b_pwm_pwm2_funcs[] = { 0, };
  225. static int jz4725b_pwm_pwm3_funcs[] = { 0, };
  226. static int jz4725b_pwm_pwm4_funcs[] = { 0, };
  227. static int jz4725b_pwm_pwm5_funcs[] = { 0, };
  228. static const struct group_desc jz4725b_groups[] = {
  229. INGENIC_PIN_GROUP("mmc0-1bit", jz4725b_mmc0_1bit),
  230. INGENIC_PIN_GROUP("mmc0-4bit", jz4725b_mmc0_4bit),
  231. INGENIC_PIN_GROUP("mmc1-1bit", jz4725b_mmc1_1bit),
  232. INGENIC_PIN_GROUP("mmc1-4bit", jz4725b_mmc1_4bit),
  233. INGENIC_PIN_GROUP("uart-data", jz4725b_uart_data),
  234. INGENIC_PIN_GROUP("nand-cs1", jz4725b_nand_cs1),
  235. INGENIC_PIN_GROUP("nand-cs2", jz4725b_nand_cs2),
  236. INGENIC_PIN_GROUP("nand-cs3", jz4725b_nand_cs3),
  237. INGENIC_PIN_GROUP("nand-cs4", jz4725b_nand_cs4),
  238. INGENIC_PIN_GROUP("nand-cle-ale", jz4725b_nand_cle_ale),
  239. INGENIC_PIN_GROUP("nand-fre-fwe", jz4725b_nand_fre_fwe),
  240. INGENIC_PIN_GROUP("pwm0", jz4725b_pwm_pwm0),
  241. INGENIC_PIN_GROUP("pwm1", jz4725b_pwm_pwm1),
  242. INGENIC_PIN_GROUP("pwm2", jz4725b_pwm_pwm2),
  243. INGENIC_PIN_GROUP("pwm3", jz4725b_pwm_pwm3),
  244. INGENIC_PIN_GROUP("pwm4", jz4725b_pwm_pwm4),
  245. INGENIC_PIN_GROUP("pwm5", jz4725b_pwm_pwm5),
  246. };
  247. static const char *jz4725b_mmc0_groups[] = { "mmc0-1bit", "mmc0-4bit", };
  248. static const char *jz4725b_mmc1_groups[] = { "mmc1-1bit", "mmc1-4bit", };
  249. static const char *jz4725b_uart_groups[] = { "uart-data", };
  250. static const char *jz4725b_nand_groups[] = {
  251. "nand-cs1", "nand-cs2", "nand-cs3", "nand-cs4",
  252. "nand-cle-ale", "nand-fre-fwe",
  253. };
  254. static const char *jz4725b_pwm0_groups[] = { "pwm0", };
  255. static const char *jz4725b_pwm1_groups[] = { "pwm1", };
  256. static const char *jz4725b_pwm2_groups[] = { "pwm2", };
  257. static const char *jz4725b_pwm3_groups[] = { "pwm3", };
  258. static const char *jz4725b_pwm4_groups[] = { "pwm4", };
  259. static const char *jz4725b_pwm5_groups[] = { "pwm5", };
  260. static const struct function_desc jz4725b_functions[] = {
  261. { "mmc0", jz4725b_mmc0_groups, ARRAY_SIZE(jz4725b_mmc0_groups), },
  262. { "mmc1", jz4725b_mmc1_groups, ARRAY_SIZE(jz4725b_mmc1_groups), },
  263. { "uart", jz4725b_uart_groups, ARRAY_SIZE(jz4725b_uart_groups), },
  264. { "nand", jz4725b_nand_groups, ARRAY_SIZE(jz4725b_nand_groups), },
  265. { "pwm0", jz4725b_pwm0_groups, ARRAY_SIZE(jz4725b_pwm0_groups), },
  266. { "pwm1", jz4725b_pwm1_groups, ARRAY_SIZE(jz4725b_pwm1_groups), },
  267. { "pwm2", jz4725b_pwm2_groups, ARRAY_SIZE(jz4725b_pwm2_groups), },
  268. { "pwm3", jz4725b_pwm3_groups, ARRAY_SIZE(jz4725b_pwm3_groups), },
  269. { "pwm4", jz4725b_pwm4_groups, ARRAY_SIZE(jz4725b_pwm4_groups), },
  270. { "pwm5", jz4725b_pwm5_groups, ARRAY_SIZE(jz4725b_pwm5_groups), },
  271. };
  272. static const struct ingenic_chip_info jz4725b_chip_info = {
  273. .num_chips = 4,
  274. .groups = jz4725b_groups,
  275. .num_groups = ARRAY_SIZE(jz4725b_groups),
  276. .functions = jz4725b_functions,
  277. .num_functions = ARRAY_SIZE(jz4725b_functions),
  278. .pull_ups = jz4740_pull_ups,
  279. .pull_downs = jz4740_pull_downs,
  280. };
  281. static const u32 jz4770_pull_ups[6] = {
  282. 0x3fffffff, 0xfff0030c, 0xffffffff, 0xffff4fff, 0xfffffb7c, 0xffa7f00f,
  283. };
  284. static const u32 jz4770_pull_downs[6] = {
  285. 0x00000000, 0x000f0c03, 0x00000000, 0x0000b000, 0x00000483, 0x00580ff0,
  286. };
  287. static int jz4770_uart0_data_pins[] = { 0xa0, 0xa3, };
  288. static int jz4770_uart0_hwflow_pins[] = { 0xa1, 0xa2, };
  289. static int jz4770_uart1_data_pins[] = { 0x7a, 0x7c, };
  290. static int jz4770_uart1_hwflow_pins[] = { 0x7b, 0x7d, };
  291. static int jz4770_uart2_data_pins[] = { 0x66, 0x67, };
  292. static int jz4770_uart2_hwflow_pins[] = { 0x65, 0x64, };
  293. static int jz4770_uart3_data_pins[] = { 0x6c, 0x85, };
  294. static int jz4770_uart3_hwflow_pins[] = { 0x88, 0x89, };
  295. static int jz4770_uart4_data_pins[] = { 0x54, 0x4a, };
  296. static int jz4770_mmc0_8bit_a_pins[] = { 0x04, 0x05, 0x06, 0x07, 0x18, };
  297. static int jz4770_mmc0_4bit_a_pins[] = { 0x15, 0x16, 0x17, };
  298. static int jz4770_mmc0_1bit_a_pins[] = { 0x12, 0x13, 0x14, };
  299. static int jz4770_mmc0_4bit_e_pins[] = { 0x95, 0x96, 0x97, };
  300. static int jz4770_mmc0_1bit_e_pins[] = { 0x9c, 0x9d, 0x94, };
  301. static int jz4770_mmc1_4bit_d_pins[] = { 0x75, 0x76, 0x77, };
  302. static int jz4770_mmc1_1bit_d_pins[] = { 0x78, 0x79, 0x74, };
  303. static int jz4770_mmc1_4bit_e_pins[] = { 0x95, 0x96, 0x97, };
  304. static int jz4770_mmc1_1bit_e_pins[] = { 0x9c, 0x9d, 0x94, };
  305. static int jz4770_nemc_data_pins[] = {
  306. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  307. };
  308. static int jz4770_nemc_cle_ale_pins[] = { 0x20, 0x21, };
  309. static int jz4770_nemc_addr_pins[] = { 0x22, 0x23, 0x24, 0x25, };
  310. static int jz4770_nemc_rd_we_pins[] = { 0x10, 0x11, };
  311. static int jz4770_nemc_frd_fwe_pins[] = { 0x12, 0x13, };
  312. static int jz4770_nemc_cs1_pins[] = { 0x15, };
  313. static int jz4770_nemc_cs2_pins[] = { 0x16, };
  314. static int jz4770_nemc_cs3_pins[] = { 0x17, };
  315. static int jz4770_nemc_cs4_pins[] = { 0x18, };
  316. static int jz4770_nemc_cs5_pins[] = { 0x19, };
  317. static int jz4770_nemc_cs6_pins[] = { 0x1a, };
  318. static int jz4770_i2c0_pins[] = { 0x6e, 0x6f, };
  319. static int jz4770_i2c1_pins[] = { 0x8e, 0x8f, };
  320. static int jz4770_i2c2_pins[] = { 0xb0, 0xb1, };
  321. static int jz4770_i2c3_pins[] = { 0x6a, 0x6b, };
  322. static int jz4770_i2c4_e_pins[] = { 0x8c, 0x8d, };
  323. static int jz4770_i2c4_f_pins[] = { 0xb9, 0xb8, };
  324. static int jz4770_cim_pins[] = {
  325. 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31,
  326. };
  327. static int jz4770_lcd_32bit_pins[] = {
  328. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  329. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  330. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  331. 0x58, 0x59, 0x51,
  332. };
  333. static int jz4770_pwm_pwm0_pins[] = { 0x80, };
  334. static int jz4770_pwm_pwm1_pins[] = { 0x81, };
  335. static int jz4770_pwm_pwm2_pins[] = { 0x82, };
  336. static int jz4770_pwm_pwm3_pins[] = { 0x83, };
  337. static int jz4770_pwm_pwm4_pins[] = { 0x84, };
  338. static int jz4770_pwm_pwm5_pins[] = { 0x85, };
  339. static int jz4770_pwm_pwm6_pins[] = { 0x6a, };
  340. static int jz4770_pwm_pwm7_pins[] = { 0x6b, };
  341. static int jz4770_uart0_data_funcs[] = { 0, 0, };
  342. static int jz4770_uart0_hwflow_funcs[] = { 0, 0, };
  343. static int jz4770_uart1_data_funcs[] = { 0, 0, };
  344. static int jz4770_uart1_hwflow_funcs[] = { 0, 0, };
  345. static int jz4770_uart2_data_funcs[] = { 1, 1, };
  346. static int jz4770_uart2_hwflow_funcs[] = { 1, 1, };
  347. static int jz4770_uart3_data_funcs[] = { 0, 1, };
  348. static int jz4770_uart3_hwflow_funcs[] = { 0, 0, };
  349. static int jz4770_uart4_data_funcs[] = { 2, 2, };
  350. static int jz4770_mmc0_8bit_a_funcs[] = { 1, 1, 1, 1, 1, };
  351. static int jz4770_mmc0_4bit_a_funcs[] = { 1, 1, 1, };
  352. static int jz4770_mmc0_1bit_a_funcs[] = { 1, 1, 0, };
  353. static int jz4770_mmc0_4bit_e_funcs[] = { 0, 0, 0, };
  354. static int jz4770_mmc0_1bit_e_funcs[] = { 0, 0, 0, };
  355. static int jz4770_mmc1_4bit_d_funcs[] = { 0, 0, 0, };
  356. static int jz4770_mmc1_1bit_d_funcs[] = { 0, 0, 0, };
  357. static int jz4770_mmc1_4bit_e_funcs[] = { 1, 1, 1, };
  358. static int jz4770_mmc1_1bit_e_funcs[] = { 1, 1, 1, };
  359. static int jz4770_nemc_data_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, };
  360. static int jz4770_nemc_cle_ale_funcs[] = { 0, 0, };
  361. static int jz4770_nemc_addr_funcs[] = { 0, 0, 0, 0, };
  362. static int jz4770_nemc_rd_we_funcs[] = { 0, 0, };
  363. static int jz4770_nemc_frd_fwe_funcs[] = { 0, 0, };
  364. static int jz4770_nemc_cs1_funcs[] = { 0, };
  365. static int jz4770_nemc_cs2_funcs[] = { 0, };
  366. static int jz4770_nemc_cs3_funcs[] = { 0, };
  367. static int jz4770_nemc_cs4_funcs[] = { 0, };
  368. static int jz4770_nemc_cs5_funcs[] = { 0, };
  369. static int jz4770_nemc_cs6_funcs[] = { 0, };
  370. static int jz4770_i2c0_funcs[] = { 0, 0, };
  371. static int jz4770_i2c1_funcs[] = { 0, 0, };
  372. static int jz4770_i2c2_funcs[] = { 2, 2, };
  373. static int jz4770_i2c3_funcs[] = { 1, 1, };
  374. static int jz4770_i2c4_e_funcs[] = { 1, 1, };
  375. static int jz4770_i2c4_f_funcs[] = { 1, 1, };
  376. static int jz4770_cim_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
  377. static int jz4770_lcd_32bit_funcs[] = {
  378. 0, 0, 0, 0, 0, 0, 0, 0,
  379. 0, 0, 0, 0, 0, 0, 0, 0,
  380. 0, 0, 0,
  381. };
  382. static int jz4770_pwm_pwm0_funcs[] = { 0, };
  383. static int jz4770_pwm_pwm1_funcs[] = { 0, };
  384. static int jz4770_pwm_pwm2_funcs[] = { 0, };
  385. static int jz4770_pwm_pwm3_funcs[] = { 0, };
  386. static int jz4770_pwm_pwm4_funcs[] = { 0, };
  387. static int jz4770_pwm_pwm5_funcs[] = { 0, };
  388. static int jz4770_pwm_pwm6_funcs[] = { 0, };
  389. static int jz4770_pwm_pwm7_funcs[] = { 0, };
  390. static const struct group_desc jz4770_groups[] = {
  391. INGENIC_PIN_GROUP("uart0-data", jz4770_uart0_data),
  392. INGENIC_PIN_GROUP("uart0-hwflow", jz4770_uart0_hwflow),
  393. INGENIC_PIN_GROUP("uart1-data", jz4770_uart1_data),
  394. INGENIC_PIN_GROUP("uart1-hwflow", jz4770_uart1_hwflow),
  395. INGENIC_PIN_GROUP("uart2-data", jz4770_uart2_data),
  396. INGENIC_PIN_GROUP("uart2-hwflow", jz4770_uart2_hwflow),
  397. INGENIC_PIN_GROUP("uart3-data", jz4770_uart3_data),
  398. INGENIC_PIN_GROUP("uart3-hwflow", jz4770_uart3_hwflow),
  399. INGENIC_PIN_GROUP("uart4-data", jz4770_uart4_data),
  400. INGENIC_PIN_GROUP("mmc0-8bit-a", jz4770_mmc0_8bit_a),
  401. INGENIC_PIN_GROUP("mmc0-4bit-a", jz4770_mmc0_4bit_a),
  402. INGENIC_PIN_GROUP("mmc0-1bit-a", jz4770_mmc0_1bit_a),
  403. INGENIC_PIN_GROUP("mmc0-4bit-e", jz4770_mmc0_4bit_e),
  404. INGENIC_PIN_GROUP("mmc0-1bit-e", jz4770_mmc0_1bit_e),
  405. INGENIC_PIN_GROUP("mmc1-4bit-d", jz4770_mmc1_4bit_d),
  406. INGENIC_PIN_GROUP("mmc1-1bit-d", jz4770_mmc1_1bit_d),
  407. INGENIC_PIN_GROUP("mmc1-4bit-e", jz4770_mmc1_4bit_e),
  408. INGENIC_PIN_GROUP("mmc1-1bit-e", jz4770_mmc1_1bit_e),
  409. INGENIC_PIN_GROUP("nemc-data", jz4770_nemc_data),
  410. INGENIC_PIN_GROUP("nemc-cle-ale", jz4770_nemc_cle_ale),
  411. INGENIC_PIN_GROUP("nemc-addr", jz4770_nemc_addr),
  412. INGENIC_PIN_GROUP("nemc-rd-we", jz4770_nemc_rd_we),
  413. INGENIC_PIN_GROUP("nemc-frd-fwe", jz4770_nemc_frd_fwe),
  414. INGENIC_PIN_GROUP("nemc-cs1", jz4770_nemc_cs1),
  415. INGENIC_PIN_GROUP("nemc-cs2", jz4770_nemc_cs2),
  416. INGENIC_PIN_GROUP("nemc-cs3", jz4770_nemc_cs3),
  417. INGENIC_PIN_GROUP("nemc-cs4", jz4770_nemc_cs4),
  418. INGENIC_PIN_GROUP("nemc-cs5", jz4770_nemc_cs5),
  419. INGENIC_PIN_GROUP("nemc-cs6", jz4770_nemc_cs6),
  420. INGENIC_PIN_GROUP("i2c0-data", jz4770_i2c0),
  421. INGENIC_PIN_GROUP("i2c1-data", jz4770_i2c1),
  422. INGENIC_PIN_GROUP("i2c2-data", jz4770_i2c2),
  423. INGENIC_PIN_GROUP("i2c3-data", jz4770_i2c3),
  424. INGENIC_PIN_GROUP("i2c4-data-e", jz4770_i2c4_e),
  425. INGENIC_PIN_GROUP("i2c4-data-f", jz4770_i2c4_f),
  426. INGENIC_PIN_GROUP("cim-data", jz4770_cim),
  427. INGENIC_PIN_GROUP("lcd-32bit", jz4770_lcd_32bit),
  428. { "lcd-no-pins", },
  429. INGENIC_PIN_GROUP("pwm0", jz4770_pwm_pwm0),
  430. INGENIC_PIN_GROUP("pwm1", jz4770_pwm_pwm1),
  431. INGENIC_PIN_GROUP("pwm2", jz4770_pwm_pwm2),
  432. INGENIC_PIN_GROUP("pwm3", jz4770_pwm_pwm3),
  433. INGENIC_PIN_GROUP("pwm4", jz4770_pwm_pwm4),
  434. INGENIC_PIN_GROUP("pwm5", jz4770_pwm_pwm5),
  435. INGENIC_PIN_GROUP("pwm6", jz4770_pwm_pwm6),
  436. INGENIC_PIN_GROUP("pwm7", jz4770_pwm_pwm7),
  437. };
  438. static const char *jz4770_uart0_groups[] = { "uart0-data", "uart0-hwflow", };
  439. static const char *jz4770_uart1_groups[] = { "uart1-data", "uart1-hwflow", };
  440. static const char *jz4770_uart2_groups[] = { "uart2-data", "uart2-hwflow", };
  441. static const char *jz4770_uart3_groups[] = { "uart3-data", "uart3-hwflow", };
  442. static const char *jz4770_uart4_groups[] = { "uart4-data", };
  443. static const char *jz4770_mmc0_groups[] = {
  444. "mmc0-8bit-a", "mmc0-4bit-a", "mmc0-1bit-a",
  445. "mmc0-1bit-e", "mmc0-4bit-e",
  446. };
  447. static const char *jz4770_mmc1_groups[] = {
  448. "mmc1-1bit-d", "mmc1-4bit-d", "mmc1-1bit-e", "mmc1-4bit-e",
  449. };
  450. static const char *jz4770_nemc_groups[] = {
  451. "nemc-data", "nemc-cle-ale", "nemc-addr", "nemc-rd-we", "nemc-frd-fwe",
  452. };
  453. static const char *jz4770_cs1_groups[] = { "nemc-cs1", };
  454. static const char *jz4770_cs6_groups[] = { "nemc-cs6", };
  455. static const char *jz4770_i2c0_groups[] = { "i2c0-data", };
  456. static const char *jz4770_i2c1_groups[] = { "i2c1-data", };
  457. static const char *jz4770_i2c2_groups[] = { "i2c2-data", };
  458. static const char *jz4770_i2c3_groups[] = { "i2c3-data", };
  459. static const char *jz4770_i2c4_groups[] = { "i2c4-data-e", "i2c4-data-f", };
  460. static const char *jz4770_cim_groups[] = { "cim-data", };
  461. static const char *jz4770_lcd_groups[] = { "lcd-32bit", "lcd-no-pins", };
  462. static const char *jz4770_pwm0_groups[] = { "pwm0", };
  463. static const char *jz4770_pwm1_groups[] = { "pwm1", };
  464. static const char *jz4770_pwm2_groups[] = { "pwm2", };
  465. static const char *jz4770_pwm3_groups[] = { "pwm3", };
  466. static const char *jz4770_pwm4_groups[] = { "pwm4", };
  467. static const char *jz4770_pwm5_groups[] = { "pwm5", };
  468. static const char *jz4770_pwm6_groups[] = { "pwm6", };
  469. static const char *jz4770_pwm7_groups[] = { "pwm7", };
  470. static const struct function_desc jz4770_functions[] = {
  471. { "uart0", jz4770_uart0_groups, ARRAY_SIZE(jz4770_uart0_groups), },
  472. { "uart1", jz4770_uart1_groups, ARRAY_SIZE(jz4770_uart1_groups), },
  473. { "uart2", jz4770_uart2_groups, ARRAY_SIZE(jz4770_uart2_groups), },
  474. { "uart3", jz4770_uart3_groups, ARRAY_SIZE(jz4770_uart3_groups), },
  475. { "uart4", jz4770_uart4_groups, ARRAY_SIZE(jz4770_uart4_groups), },
  476. { "mmc0", jz4770_mmc0_groups, ARRAY_SIZE(jz4770_mmc0_groups), },
  477. { "mmc1", jz4770_mmc1_groups, ARRAY_SIZE(jz4770_mmc1_groups), },
  478. { "nemc", jz4770_nemc_groups, ARRAY_SIZE(jz4770_nemc_groups), },
  479. { "nemc-cs1", jz4770_cs1_groups, ARRAY_SIZE(jz4770_cs1_groups), },
  480. { "nemc-cs6", jz4770_cs6_groups, ARRAY_SIZE(jz4770_cs6_groups), },
  481. { "i2c0", jz4770_i2c0_groups, ARRAY_SIZE(jz4770_i2c0_groups), },
  482. { "i2c1", jz4770_i2c1_groups, ARRAY_SIZE(jz4770_i2c1_groups), },
  483. { "i2c2", jz4770_i2c2_groups, ARRAY_SIZE(jz4770_i2c2_groups), },
  484. { "i2c3", jz4770_i2c3_groups, ARRAY_SIZE(jz4770_i2c3_groups), },
  485. { "i2c4", jz4770_i2c4_groups, ARRAY_SIZE(jz4770_i2c4_groups), },
  486. { "cim", jz4770_cim_groups, ARRAY_SIZE(jz4770_cim_groups), },
  487. { "lcd", jz4770_lcd_groups, ARRAY_SIZE(jz4770_lcd_groups), },
  488. { "pwm0", jz4770_pwm0_groups, ARRAY_SIZE(jz4770_pwm0_groups), },
  489. { "pwm1", jz4770_pwm1_groups, ARRAY_SIZE(jz4770_pwm1_groups), },
  490. { "pwm2", jz4770_pwm2_groups, ARRAY_SIZE(jz4770_pwm2_groups), },
  491. { "pwm3", jz4770_pwm3_groups, ARRAY_SIZE(jz4770_pwm3_groups), },
  492. { "pwm4", jz4770_pwm4_groups, ARRAY_SIZE(jz4770_pwm4_groups), },
  493. { "pwm5", jz4770_pwm5_groups, ARRAY_SIZE(jz4770_pwm5_groups), },
  494. { "pwm6", jz4770_pwm6_groups, ARRAY_SIZE(jz4770_pwm6_groups), },
  495. { "pwm7", jz4770_pwm7_groups, ARRAY_SIZE(jz4770_pwm7_groups), },
  496. };
  497. static const struct ingenic_chip_info jz4770_chip_info = {
  498. .num_chips = 6,
  499. .groups = jz4770_groups,
  500. .num_groups = ARRAY_SIZE(jz4770_groups),
  501. .functions = jz4770_functions,
  502. .num_functions = ARRAY_SIZE(jz4770_functions),
  503. .pull_ups = jz4770_pull_ups,
  504. .pull_downs = jz4770_pull_downs,
  505. };
  506. static u32 gpio_ingenic_read_reg(struct ingenic_gpio_chip *jzgc, u8 reg)
  507. {
  508. unsigned int val;
  509. regmap_read(jzgc->jzpc->map, jzgc->reg_base + reg, &val);
  510. return (u32) val;
  511. }
  512. static void gpio_ingenic_set_bit(struct ingenic_gpio_chip *jzgc,
  513. u8 reg, u8 offset, bool set)
  514. {
  515. if (set)
  516. reg = REG_SET(reg);
  517. else
  518. reg = REG_CLEAR(reg);
  519. regmap_write(jzgc->jzpc->map, jzgc->reg_base + reg, BIT(offset));
  520. }
  521. static inline bool ingenic_gpio_get_value(struct ingenic_gpio_chip *jzgc,
  522. u8 offset)
  523. {
  524. unsigned int val = gpio_ingenic_read_reg(jzgc, GPIO_PIN);
  525. return !!(val & BIT(offset));
  526. }
  527. static void ingenic_gpio_set_value(struct ingenic_gpio_chip *jzgc,
  528. u8 offset, int value)
  529. {
  530. if (jzgc->jzpc->version >= ID_JZ4770)
  531. gpio_ingenic_set_bit(jzgc, JZ4770_GPIO_PAT0, offset, !!value);
  532. else
  533. gpio_ingenic_set_bit(jzgc, JZ4740_GPIO_DATA, offset, !!value);
  534. }
  535. static void irq_set_type(struct ingenic_gpio_chip *jzgc,
  536. u8 offset, unsigned int type)
  537. {
  538. u8 reg1, reg2;
  539. if (jzgc->jzpc->version >= ID_JZ4770) {
  540. reg1 = JZ4770_GPIO_PAT1;
  541. reg2 = JZ4770_GPIO_PAT0;
  542. } else {
  543. reg1 = JZ4740_GPIO_TRIG;
  544. reg2 = JZ4740_GPIO_DIR;
  545. }
  546. switch (type) {
  547. case IRQ_TYPE_EDGE_RISING:
  548. gpio_ingenic_set_bit(jzgc, reg2, offset, true);
  549. gpio_ingenic_set_bit(jzgc, reg1, offset, true);
  550. break;
  551. case IRQ_TYPE_EDGE_FALLING:
  552. gpio_ingenic_set_bit(jzgc, reg2, offset, false);
  553. gpio_ingenic_set_bit(jzgc, reg1, offset, true);
  554. break;
  555. case IRQ_TYPE_LEVEL_HIGH:
  556. gpio_ingenic_set_bit(jzgc, reg2, offset, true);
  557. gpio_ingenic_set_bit(jzgc, reg1, offset, false);
  558. break;
  559. case IRQ_TYPE_LEVEL_LOW:
  560. default:
  561. gpio_ingenic_set_bit(jzgc, reg2, offset, false);
  562. gpio_ingenic_set_bit(jzgc, reg1, offset, false);
  563. break;
  564. }
  565. }
  566. static void ingenic_gpio_irq_mask(struct irq_data *irqd)
  567. {
  568. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  569. struct ingenic_gpio_chip *jzgc = gpiochip_get_data(gc);
  570. gpio_ingenic_set_bit(jzgc, GPIO_MSK, irqd->hwirq, true);
  571. }
  572. static void ingenic_gpio_irq_unmask(struct irq_data *irqd)
  573. {
  574. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  575. struct ingenic_gpio_chip *jzgc = gpiochip_get_data(gc);
  576. gpio_ingenic_set_bit(jzgc, GPIO_MSK, irqd->hwirq, false);
  577. }
  578. static void ingenic_gpio_irq_enable(struct irq_data *irqd)
  579. {
  580. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  581. struct ingenic_gpio_chip *jzgc = gpiochip_get_data(gc);
  582. int irq = irqd->hwirq;
  583. if (jzgc->jzpc->version >= ID_JZ4770)
  584. gpio_ingenic_set_bit(jzgc, JZ4770_GPIO_INT, irq, true);
  585. else
  586. gpio_ingenic_set_bit(jzgc, JZ4740_GPIO_SELECT, irq, true);
  587. ingenic_gpio_irq_unmask(irqd);
  588. }
  589. static void ingenic_gpio_irq_disable(struct irq_data *irqd)
  590. {
  591. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  592. struct ingenic_gpio_chip *jzgc = gpiochip_get_data(gc);
  593. int irq = irqd->hwirq;
  594. ingenic_gpio_irq_mask(irqd);
  595. if (jzgc->jzpc->version >= ID_JZ4770)
  596. gpio_ingenic_set_bit(jzgc, JZ4770_GPIO_INT, irq, false);
  597. else
  598. gpio_ingenic_set_bit(jzgc, JZ4740_GPIO_SELECT, irq, false);
  599. }
  600. static void ingenic_gpio_irq_ack(struct irq_data *irqd)
  601. {
  602. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  603. struct ingenic_gpio_chip *jzgc = gpiochip_get_data(gc);
  604. int irq = irqd->hwirq;
  605. bool high;
  606. if (irqd_get_trigger_type(irqd) == IRQ_TYPE_EDGE_BOTH) {
  607. /*
  608. * Switch to an interrupt for the opposite edge to the one that
  609. * triggered the interrupt being ACKed.
  610. */
  611. high = ingenic_gpio_get_value(jzgc, irq);
  612. if (high)
  613. irq_set_type(jzgc, irq, IRQ_TYPE_EDGE_FALLING);
  614. else
  615. irq_set_type(jzgc, irq, IRQ_TYPE_EDGE_RISING);
  616. }
  617. if (jzgc->jzpc->version >= ID_JZ4770)
  618. gpio_ingenic_set_bit(jzgc, JZ4770_GPIO_FLAG, irq, false);
  619. else
  620. gpio_ingenic_set_bit(jzgc, JZ4740_GPIO_DATA, irq, true);
  621. }
  622. static int ingenic_gpio_irq_set_type(struct irq_data *irqd, unsigned int type)
  623. {
  624. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  625. struct ingenic_gpio_chip *jzgc = gpiochip_get_data(gc);
  626. switch (type) {
  627. case IRQ_TYPE_EDGE_BOTH:
  628. case IRQ_TYPE_EDGE_RISING:
  629. case IRQ_TYPE_EDGE_FALLING:
  630. irq_set_handler_locked(irqd, handle_edge_irq);
  631. break;
  632. case IRQ_TYPE_LEVEL_HIGH:
  633. case IRQ_TYPE_LEVEL_LOW:
  634. irq_set_handler_locked(irqd, handle_level_irq);
  635. break;
  636. default:
  637. irq_set_handler_locked(irqd, handle_bad_irq);
  638. }
  639. if (type == IRQ_TYPE_EDGE_BOTH) {
  640. /*
  641. * The hardware does not support interrupts on both edges. The
  642. * best we can do is to set up a single-edge interrupt and then
  643. * switch to the opposing edge when ACKing the interrupt.
  644. */
  645. bool high = ingenic_gpio_get_value(jzgc, irqd->hwirq);
  646. type = high ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
  647. }
  648. irq_set_type(jzgc, irqd->hwirq, type);
  649. return 0;
  650. }
  651. static int ingenic_gpio_irq_set_wake(struct irq_data *irqd, unsigned int on)
  652. {
  653. struct gpio_chip *gc = irq_data_get_irq_chip_data(irqd);
  654. struct ingenic_gpio_chip *jzgc = gpiochip_get_data(gc);
  655. return irq_set_irq_wake(jzgc->irq, on);
  656. }
  657. static void ingenic_gpio_irq_handler(struct irq_desc *desc)
  658. {
  659. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  660. struct ingenic_gpio_chip *jzgc = gpiochip_get_data(gc);
  661. struct irq_chip *irq_chip = irq_data_get_irq_chip(&desc->irq_data);
  662. unsigned long flag, i;
  663. chained_irq_enter(irq_chip, desc);
  664. if (jzgc->jzpc->version >= ID_JZ4770)
  665. flag = gpio_ingenic_read_reg(jzgc, JZ4770_GPIO_FLAG);
  666. else
  667. flag = gpio_ingenic_read_reg(jzgc, JZ4740_GPIO_FLAG);
  668. for_each_set_bit(i, &flag, 32)
  669. generic_handle_irq(irq_linear_revmap(gc->irq.domain, i));
  670. chained_irq_exit(irq_chip, desc);
  671. }
  672. static void ingenic_gpio_set(struct gpio_chip *gc,
  673. unsigned int offset, int value)
  674. {
  675. struct ingenic_gpio_chip *jzgc = gpiochip_get_data(gc);
  676. ingenic_gpio_set_value(jzgc, offset, value);
  677. }
  678. static int ingenic_gpio_get(struct gpio_chip *gc, unsigned int offset)
  679. {
  680. struct ingenic_gpio_chip *jzgc = gpiochip_get_data(gc);
  681. return (int) ingenic_gpio_get_value(jzgc, offset);
  682. }
  683. static int ingenic_gpio_direction_input(struct gpio_chip *gc,
  684. unsigned int offset)
  685. {
  686. return pinctrl_gpio_direction_input(gc->base + offset);
  687. }
  688. static int ingenic_gpio_direction_output(struct gpio_chip *gc,
  689. unsigned int offset, int value)
  690. {
  691. ingenic_gpio_set(gc, offset, value);
  692. return pinctrl_gpio_direction_output(gc->base + offset);
  693. }
  694. static inline void ingenic_config_pin(struct ingenic_pinctrl *jzpc,
  695. unsigned int pin, u8 reg, bool set)
  696. {
  697. unsigned int idx = pin % PINS_PER_GPIO_CHIP;
  698. unsigned int offt = pin / PINS_PER_GPIO_CHIP;
  699. regmap_write(jzpc->map, offt * 0x100 +
  700. (set ? REG_SET(reg) : REG_CLEAR(reg)), BIT(idx));
  701. }
  702. static inline bool ingenic_get_pin_config(struct ingenic_pinctrl *jzpc,
  703. unsigned int pin, u8 reg)
  704. {
  705. unsigned int idx = pin % PINS_PER_GPIO_CHIP;
  706. unsigned int offt = pin / PINS_PER_GPIO_CHIP;
  707. unsigned int val;
  708. regmap_read(jzpc->map, offt * 0x100 + reg, &val);
  709. return val & BIT(idx);
  710. }
  711. static int ingenic_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  712. {
  713. struct ingenic_gpio_chip *jzgc = gpiochip_get_data(gc);
  714. struct ingenic_pinctrl *jzpc = jzgc->jzpc;
  715. unsigned int pin = gc->base + offset;
  716. if (jzpc->version >= ID_JZ4770)
  717. return ingenic_get_pin_config(jzpc, pin, JZ4770_GPIO_PAT1);
  718. if (ingenic_get_pin_config(jzpc, pin, JZ4740_GPIO_SELECT))
  719. return true;
  720. return !ingenic_get_pin_config(jzpc, pin, JZ4740_GPIO_DIR);
  721. }
  722. static const struct pinctrl_ops ingenic_pctlops = {
  723. .get_groups_count = pinctrl_generic_get_group_count,
  724. .get_group_name = pinctrl_generic_get_group_name,
  725. .get_group_pins = pinctrl_generic_get_group_pins,
  726. .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
  727. .dt_free_map = pinconf_generic_dt_free_map,
  728. };
  729. static int ingenic_pinmux_set_pin_fn(struct ingenic_pinctrl *jzpc,
  730. int pin, int func)
  731. {
  732. unsigned int idx = pin % PINS_PER_GPIO_CHIP;
  733. unsigned int offt = pin / PINS_PER_GPIO_CHIP;
  734. dev_dbg(jzpc->dev, "set pin P%c%u to function %u\n",
  735. 'A' + offt, idx, func);
  736. if (jzpc->version >= ID_JZ4770) {
  737. ingenic_config_pin(jzpc, pin, JZ4770_GPIO_INT, false);
  738. ingenic_config_pin(jzpc, pin, GPIO_MSK, false);
  739. ingenic_config_pin(jzpc, pin, JZ4770_GPIO_PAT1, func & 0x2);
  740. ingenic_config_pin(jzpc, pin, JZ4770_GPIO_PAT0, func & 0x1);
  741. } else {
  742. ingenic_config_pin(jzpc, pin, JZ4740_GPIO_FUNC, true);
  743. ingenic_config_pin(jzpc, pin, JZ4740_GPIO_TRIG, func & 0x2);
  744. ingenic_config_pin(jzpc, pin, JZ4740_GPIO_SELECT, func > 0);
  745. }
  746. return 0;
  747. }
  748. static int ingenic_pinmux_set_mux(struct pinctrl_dev *pctldev,
  749. unsigned int selector, unsigned int group)
  750. {
  751. struct ingenic_pinctrl *jzpc = pinctrl_dev_get_drvdata(pctldev);
  752. struct function_desc *func;
  753. struct group_desc *grp;
  754. unsigned int i;
  755. func = pinmux_generic_get_function(pctldev, selector);
  756. if (!func)
  757. return -EINVAL;
  758. grp = pinctrl_generic_get_group(pctldev, group);
  759. if (!grp)
  760. return -EINVAL;
  761. dev_dbg(pctldev->dev, "enable function %s group %s\n",
  762. func->name, grp->name);
  763. for (i = 0; i < grp->num_pins; i++) {
  764. int *pin_modes = grp->data;
  765. ingenic_pinmux_set_pin_fn(jzpc, grp->pins[i], pin_modes[i]);
  766. }
  767. return 0;
  768. }
  769. static int ingenic_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
  770. struct pinctrl_gpio_range *range,
  771. unsigned int pin, bool input)
  772. {
  773. struct ingenic_pinctrl *jzpc = pinctrl_dev_get_drvdata(pctldev);
  774. unsigned int idx = pin % PINS_PER_GPIO_CHIP;
  775. unsigned int offt = pin / PINS_PER_GPIO_CHIP;
  776. dev_dbg(pctldev->dev, "set pin P%c%u to %sput\n",
  777. 'A' + offt, idx, input ? "in" : "out");
  778. if (jzpc->version >= ID_JZ4770) {
  779. ingenic_config_pin(jzpc, pin, JZ4770_GPIO_INT, false);
  780. ingenic_config_pin(jzpc, pin, GPIO_MSK, true);
  781. ingenic_config_pin(jzpc, pin, JZ4770_GPIO_PAT1, input);
  782. } else {
  783. ingenic_config_pin(jzpc, pin, JZ4740_GPIO_SELECT, false);
  784. ingenic_config_pin(jzpc, pin, JZ4740_GPIO_DIR, !input);
  785. ingenic_config_pin(jzpc, pin, JZ4740_GPIO_FUNC, false);
  786. }
  787. return 0;
  788. }
  789. static const struct pinmux_ops ingenic_pmxops = {
  790. .get_functions_count = pinmux_generic_get_function_count,
  791. .get_function_name = pinmux_generic_get_function_name,
  792. .get_function_groups = pinmux_generic_get_function_groups,
  793. .set_mux = ingenic_pinmux_set_mux,
  794. .gpio_set_direction = ingenic_pinmux_gpio_set_direction,
  795. };
  796. static int ingenic_pinconf_get(struct pinctrl_dev *pctldev,
  797. unsigned int pin, unsigned long *config)
  798. {
  799. struct ingenic_pinctrl *jzpc = pinctrl_dev_get_drvdata(pctldev);
  800. enum pin_config_param param = pinconf_to_config_param(*config);
  801. unsigned int idx = pin % PINS_PER_GPIO_CHIP;
  802. unsigned int offt = pin / PINS_PER_GPIO_CHIP;
  803. bool pull;
  804. if (jzpc->version >= ID_JZ4770)
  805. pull = !ingenic_get_pin_config(jzpc, pin, JZ4770_GPIO_PEN);
  806. else
  807. pull = !ingenic_get_pin_config(jzpc, pin, JZ4740_GPIO_PULL_DIS);
  808. switch (param) {
  809. case PIN_CONFIG_BIAS_DISABLE:
  810. if (pull)
  811. return -EINVAL;
  812. break;
  813. case PIN_CONFIG_BIAS_PULL_UP:
  814. if (!pull || !(jzpc->info->pull_ups[offt] & BIT(idx)))
  815. return -EINVAL;
  816. break;
  817. case PIN_CONFIG_BIAS_PULL_DOWN:
  818. if (!pull || !(jzpc->info->pull_downs[offt] & BIT(idx)))
  819. return -EINVAL;
  820. break;
  821. default:
  822. return -ENOTSUPP;
  823. }
  824. *config = pinconf_to_config_packed(param, 1);
  825. return 0;
  826. }
  827. static void ingenic_set_bias(struct ingenic_pinctrl *jzpc,
  828. unsigned int pin, bool enabled)
  829. {
  830. if (jzpc->version >= ID_JZ4770)
  831. ingenic_config_pin(jzpc, pin, JZ4770_GPIO_PEN, !enabled);
  832. else
  833. ingenic_config_pin(jzpc, pin, JZ4740_GPIO_PULL_DIS, !enabled);
  834. }
  835. static int ingenic_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  836. unsigned long *configs, unsigned int num_configs)
  837. {
  838. struct ingenic_pinctrl *jzpc = pinctrl_dev_get_drvdata(pctldev);
  839. unsigned int idx = pin % PINS_PER_GPIO_CHIP;
  840. unsigned int offt = pin / PINS_PER_GPIO_CHIP;
  841. unsigned int cfg;
  842. for (cfg = 0; cfg < num_configs; cfg++) {
  843. switch (pinconf_to_config_param(configs[cfg])) {
  844. case PIN_CONFIG_BIAS_DISABLE:
  845. case PIN_CONFIG_BIAS_PULL_UP:
  846. case PIN_CONFIG_BIAS_PULL_DOWN:
  847. continue;
  848. default:
  849. return -ENOTSUPP;
  850. }
  851. }
  852. for (cfg = 0; cfg < num_configs; cfg++) {
  853. switch (pinconf_to_config_param(configs[cfg])) {
  854. case PIN_CONFIG_BIAS_DISABLE:
  855. dev_dbg(jzpc->dev, "disable pull-over for pin P%c%u\n",
  856. 'A' + offt, idx);
  857. ingenic_set_bias(jzpc, pin, false);
  858. break;
  859. case PIN_CONFIG_BIAS_PULL_UP:
  860. if (!(jzpc->info->pull_ups[offt] & BIT(idx)))
  861. return -EINVAL;
  862. dev_dbg(jzpc->dev, "set pull-up for pin P%c%u\n",
  863. 'A' + offt, idx);
  864. ingenic_set_bias(jzpc, pin, true);
  865. break;
  866. case PIN_CONFIG_BIAS_PULL_DOWN:
  867. if (!(jzpc->info->pull_downs[offt] & BIT(idx)))
  868. return -EINVAL;
  869. dev_dbg(jzpc->dev, "set pull-down for pin P%c%u\n",
  870. 'A' + offt, idx);
  871. ingenic_set_bias(jzpc, pin, true);
  872. break;
  873. default:
  874. unreachable();
  875. }
  876. }
  877. return 0;
  878. }
  879. static int ingenic_pinconf_group_get(struct pinctrl_dev *pctldev,
  880. unsigned int group, unsigned long *config)
  881. {
  882. const unsigned int *pins;
  883. unsigned int i, npins, old = 0;
  884. int ret;
  885. ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
  886. if (ret)
  887. return ret;
  888. for (i = 0; i < npins; i++) {
  889. if (ingenic_pinconf_get(pctldev, pins[i], config))
  890. return -ENOTSUPP;
  891. /* configs do not match between two pins */
  892. if (i && (old != *config))
  893. return -ENOTSUPP;
  894. old = *config;
  895. }
  896. return 0;
  897. }
  898. static int ingenic_pinconf_group_set(struct pinctrl_dev *pctldev,
  899. unsigned int group, unsigned long *configs,
  900. unsigned int num_configs)
  901. {
  902. const unsigned int *pins;
  903. unsigned int i, npins;
  904. int ret;
  905. ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
  906. if (ret)
  907. return ret;
  908. for (i = 0; i < npins; i++) {
  909. ret = ingenic_pinconf_set(pctldev,
  910. pins[i], configs, num_configs);
  911. if (ret)
  912. return ret;
  913. }
  914. return 0;
  915. }
  916. static const struct pinconf_ops ingenic_confops = {
  917. .is_generic = true,
  918. .pin_config_get = ingenic_pinconf_get,
  919. .pin_config_set = ingenic_pinconf_set,
  920. .pin_config_group_get = ingenic_pinconf_group_get,
  921. .pin_config_group_set = ingenic_pinconf_group_set,
  922. };
  923. static const struct regmap_config ingenic_pinctrl_regmap_config = {
  924. .reg_bits = 32,
  925. .val_bits = 32,
  926. .reg_stride = 4,
  927. };
  928. static const struct of_device_id ingenic_pinctrl_of_match[] = {
  929. { .compatible = "ingenic,jz4740-pinctrl", .data = (void *) ID_JZ4740 },
  930. { .compatible = "ingenic,jz4725b-pinctrl", .data = (void *)ID_JZ4725B },
  931. { .compatible = "ingenic,jz4770-pinctrl", .data = (void *) ID_JZ4770 },
  932. { .compatible = "ingenic,jz4780-pinctrl", .data = (void *) ID_JZ4780 },
  933. {},
  934. };
  935. static const struct of_device_id ingenic_gpio_of_match[] __initconst = {
  936. { .compatible = "ingenic,jz4740-gpio", },
  937. { .compatible = "ingenic,jz4770-gpio", },
  938. { .compatible = "ingenic,jz4780-gpio", },
  939. {},
  940. };
  941. static int __init ingenic_gpio_probe(struct ingenic_pinctrl *jzpc,
  942. struct device_node *node)
  943. {
  944. struct ingenic_gpio_chip *jzgc;
  945. struct device *dev = jzpc->dev;
  946. unsigned int bank;
  947. int err;
  948. err = of_property_read_u32(node, "reg", &bank);
  949. if (err) {
  950. dev_err(dev, "Cannot read \"reg\" property: %i\n", err);
  951. return err;
  952. }
  953. jzgc = devm_kzalloc(dev, sizeof(*jzgc), GFP_KERNEL);
  954. if (!jzgc)
  955. return -ENOMEM;
  956. jzgc->jzpc = jzpc;
  957. jzgc->reg_base = bank * 0x100;
  958. jzgc->gc.label = devm_kasprintf(dev, GFP_KERNEL, "GPIO%c", 'A' + bank);
  959. if (!jzgc->gc.label)
  960. return -ENOMEM;
  961. /* DO NOT EXPAND THIS: FOR BACKWARD GPIO NUMBERSPACE COMPATIBIBILITY
  962. * ONLY: WORK TO TRANSITION CONSUMERS TO USE THE GPIO DESCRIPTOR API IN
  963. * <linux/gpio/consumer.h> INSTEAD.
  964. */
  965. jzgc->gc.base = bank * 32;
  966. jzgc->gc.ngpio = 32;
  967. jzgc->gc.parent = dev;
  968. jzgc->gc.of_node = node;
  969. jzgc->gc.owner = THIS_MODULE;
  970. jzgc->gc.set = ingenic_gpio_set;
  971. jzgc->gc.get = ingenic_gpio_get;
  972. jzgc->gc.direction_input = ingenic_gpio_direction_input;
  973. jzgc->gc.direction_output = ingenic_gpio_direction_output;
  974. jzgc->gc.get_direction = ingenic_gpio_get_direction;
  975. if (of_property_read_bool(node, "gpio-ranges")) {
  976. jzgc->gc.request = gpiochip_generic_request;
  977. jzgc->gc.free = gpiochip_generic_free;
  978. }
  979. err = devm_gpiochip_add_data(dev, &jzgc->gc, jzgc);
  980. if (err)
  981. return err;
  982. jzgc->irq = irq_of_parse_and_map(node, 0);
  983. if (!jzgc->irq)
  984. return -EINVAL;
  985. jzgc->irq_chip.name = jzgc->gc.label;
  986. jzgc->irq_chip.irq_enable = ingenic_gpio_irq_enable;
  987. jzgc->irq_chip.irq_disable = ingenic_gpio_irq_disable;
  988. jzgc->irq_chip.irq_unmask = ingenic_gpio_irq_unmask;
  989. jzgc->irq_chip.irq_mask = ingenic_gpio_irq_mask;
  990. jzgc->irq_chip.irq_ack = ingenic_gpio_irq_ack;
  991. jzgc->irq_chip.irq_set_type = ingenic_gpio_irq_set_type;
  992. jzgc->irq_chip.irq_set_wake = ingenic_gpio_irq_set_wake;
  993. jzgc->irq_chip.flags = IRQCHIP_MASK_ON_SUSPEND;
  994. err = gpiochip_irqchip_add(&jzgc->gc, &jzgc->irq_chip, 0,
  995. handle_level_irq, IRQ_TYPE_NONE);
  996. if (err)
  997. return err;
  998. gpiochip_set_chained_irqchip(&jzgc->gc, &jzgc->irq_chip,
  999. jzgc->irq, ingenic_gpio_irq_handler);
  1000. return 0;
  1001. }
  1002. static int __init ingenic_pinctrl_probe(struct platform_device *pdev)
  1003. {
  1004. struct device *dev = &pdev->dev;
  1005. struct ingenic_pinctrl *jzpc;
  1006. struct pinctrl_desc *pctl_desc;
  1007. void __iomem *base;
  1008. const struct platform_device_id *id = platform_get_device_id(pdev);
  1009. const struct of_device_id *of_id = of_match_device(
  1010. ingenic_pinctrl_of_match, dev);
  1011. const struct ingenic_chip_info *chip_info;
  1012. struct device_node *node;
  1013. unsigned int i;
  1014. int err;
  1015. jzpc = devm_kzalloc(dev, sizeof(*jzpc), GFP_KERNEL);
  1016. if (!jzpc)
  1017. return -ENOMEM;
  1018. base = devm_ioremap_resource(dev,
  1019. platform_get_resource(pdev, IORESOURCE_MEM, 0));
  1020. if (IS_ERR(base))
  1021. return PTR_ERR(base);
  1022. jzpc->map = devm_regmap_init_mmio(dev, base,
  1023. &ingenic_pinctrl_regmap_config);
  1024. if (IS_ERR(jzpc->map)) {
  1025. dev_err(dev, "Failed to create regmap\n");
  1026. return PTR_ERR(jzpc->map);
  1027. }
  1028. jzpc->dev = dev;
  1029. if (of_id)
  1030. jzpc->version = (enum jz_version)of_id->data;
  1031. else
  1032. jzpc->version = (enum jz_version)id->driver_data;
  1033. if (jzpc->version >= ID_JZ4770)
  1034. chip_info = &jz4770_chip_info;
  1035. else if (jzpc->version >= ID_JZ4725B)
  1036. chip_info = &jz4725b_chip_info;
  1037. else
  1038. chip_info = &jz4740_chip_info;
  1039. jzpc->info = chip_info;
  1040. pctl_desc = devm_kzalloc(&pdev->dev, sizeof(*pctl_desc), GFP_KERNEL);
  1041. if (!pctl_desc)
  1042. return -ENOMEM;
  1043. /* fill in pinctrl_desc structure */
  1044. pctl_desc->name = dev_name(dev);
  1045. pctl_desc->owner = THIS_MODULE;
  1046. pctl_desc->pctlops = &ingenic_pctlops;
  1047. pctl_desc->pmxops = &ingenic_pmxops;
  1048. pctl_desc->confops = &ingenic_confops;
  1049. pctl_desc->npins = chip_info->num_chips * PINS_PER_GPIO_CHIP;
  1050. pctl_desc->pins = jzpc->pdesc = devm_kcalloc(&pdev->dev,
  1051. pctl_desc->npins, sizeof(*jzpc->pdesc), GFP_KERNEL);
  1052. if (!jzpc->pdesc)
  1053. return -ENOMEM;
  1054. for (i = 0; i < pctl_desc->npins; i++) {
  1055. jzpc->pdesc[i].number = i;
  1056. jzpc->pdesc[i].name = kasprintf(GFP_KERNEL, "P%c%d",
  1057. 'A' + (i / PINS_PER_GPIO_CHIP),
  1058. i % PINS_PER_GPIO_CHIP);
  1059. }
  1060. jzpc->pctl = devm_pinctrl_register(dev, pctl_desc, jzpc);
  1061. if (IS_ERR(jzpc->pctl)) {
  1062. dev_err(dev, "Failed to register pinctrl\n");
  1063. return PTR_ERR(jzpc->pctl);
  1064. }
  1065. for (i = 0; i < chip_info->num_groups; i++) {
  1066. const struct group_desc *group = &chip_info->groups[i];
  1067. err = pinctrl_generic_add_group(jzpc->pctl, group->name,
  1068. group->pins, group->num_pins, group->data);
  1069. if (err < 0) {
  1070. dev_err(dev, "Failed to register group %s\n",
  1071. group->name);
  1072. return err;
  1073. }
  1074. }
  1075. for (i = 0; i < chip_info->num_functions; i++) {
  1076. const struct function_desc *func = &chip_info->functions[i];
  1077. err = pinmux_generic_add_function(jzpc->pctl, func->name,
  1078. func->group_names, func->num_group_names,
  1079. func->data);
  1080. if (err < 0) {
  1081. dev_err(dev, "Failed to register function %s\n",
  1082. func->name);
  1083. return err;
  1084. }
  1085. }
  1086. dev_set_drvdata(dev, jzpc->map);
  1087. for_each_child_of_node(dev->of_node, node) {
  1088. if (of_match_node(ingenic_gpio_of_match, node)) {
  1089. err = ingenic_gpio_probe(jzpc, node);
  1090. if (err)
  1091. return err;
  1092. }
  1093. }
  1094. return 0;
  1095. }
  1096. static const struct platform_device_id ingenic_pinctrl_ids[] = {
  1097. { "jz4740-pinctrl", ID_JZ4740 },
  1098. { "jz4725b-pinctrl", ID_JZ4725B },
  1099. { "jz4770-pinctrl", ID_JZ4770 },
  1100. { "jz4780-pinctrl", ID_JZ4780 },
  1101. {},
  1102. };
  1103. static struct platform_driver ingenic_pinctrl_driver = {
  1104. .driver = {
  1105. .name = "pinctrl-ingenic",
  1106. .of_match_table = of_match_ptr(ingenic_pinctrl_of_match),
  1107. },
  1108. .id_table = ingenic_pinctrl_ids,
  1109. };
  1110. static int __init ingenic_pinctrl_drv_register(void)
  1111. {
  1112. return platform_driver_probe(&ingenic_pinctrl_driver,
  1113. ingenic_pinctrl_probe);
  1114. }
  1115. subsys_initcall(ingenic_pinctrl_drv_register);