pinctrl-cy8c95x0.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CY8C95X0 20/40/60 pin I2C GPIO port expander with interrupt support
  4. *
  5. * Copyright (C) 2022 9elements GmbH
  6. * Authors: Patrick Rudolph <patrick.rudolph@9elements.com>
  7. * Naresh Solanki <Naresh.Solanki@9elements.com>
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/bitmap.h>
  11. #include <linux/cleanup.h>
  12. #include <linux/dmi.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/i2c.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/module.h>
  20. #include <linux/property.h>
  21. #include <linux/regmap.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/pinctrl/consumer.h>
  25. #include <linux/pinctrl/pinconf.h>
  26. #include <linux/pinctrl/pinconf-generic.h>
  27. #include <linux/pinctrl/pinctrl.h>
  28. #include <linux/pinctrl/pinmux.h>
  29. /* Fast access registers */
  30. #define CY8C95X0_INPUT 0x00
  31. #define CY8C95X0_OUTPUT 0x08
  32. #define CY8C95X0_INTSTATUS 0x10
  33. #define CY8C95X0_INPUT_(x) (CY8C95X0_INPUT + (x))
  34. #define CY8C95X0_OUTPUT_(x) (CY8C95X0_OUTPUT + (x))
  35. #define CY8C95X0_INTSTATUS_(x) (CY8C95X0_INTSTATUS + (x))
  36. /* Port Select configures the port */
  37. #define CY8C95X0_PORTSEL 0x18
  38. /* Port settings, write PORTSEL first */
  39. #define CY8C95X0_INTMASK 0x19
  40. #define CY8C95X0_SELPWM 0x1A
  41. #define CY8C95X0_INVERT 0x1B
  42. #define CY8C95X0_DIRECTION 0x1C
  43. /* Drive mode register change state on writing '1' */
  44. #define CY8C95X0_DRV_PU 0x1D
  45. #define CY8C95X0_DRV_PD 0x1E
  46. #define CY8C95X0_DRV_ODH 0x1F
  47. #define CY8C95X0_DRV_ODL 0x20
  48. #define CY8C95X0_DRV_PP_FAST 0x21
  49. #define CY8C95X0_DRV_PP_SLOW 0x22
  50. #define CY8C95X0_DRV_HIZ 0x23
  51. /* Internal device configuration */
  52. #define CY8C95X0_ENABLE_WDE 0x2D
  53. #define CY8C95X0_DEVID 0x2E
  54. #define CY8C95X0_WATCHDOG 0x2F
  55. #define CY8C95X0_COMMAND 0x30
  56. #define CY8C95X0_PIN_TO_OFFSET(x) (((x) >= 20) ? ((x) + 4) : (x))
  57. #define MAX_BANK 8
  58. #define BANK_SZ 8
  59. #define MAX_LINE (MAX_BANK * BANK_SZ)
  60. #define MUXED_STRIDE 16
  61. #define CY8C95X0_GPIO_MASK GENMASK(7, 0)
  62. #define CY8C95X0_VIRTUAL 0x40
  63. #define CY8C95X0_MUX_REGMAP_TO_OFFSET(x, p) \
  64. (CY8C95X0_VIRTUAL + (x) - CY8C95X0_PORTSEL + (p) * MUXED_STRIDE)
  65. static const struct i2c_device_id cy8c95x0_id[] = {
  66. { "cy8c9520", 20, },
  67. { "cy8c9540", 40, },
  68. { "cy8c9560", 60, },
  69. { }
  70. };
  71. MODULE_DEVICE_TABLE(i2c, cy8c95x0_id);
  72. #define OF_CY8C95X(__nrgpio) ((void *)(__nrgpio))
  73. static const struct of_device_id cy8c95x0_dt_ids[] = {
  74. { .compatible = "cypress,cy8c9520", .data = OF_CY8C95X(20), },
  75. { .compatible = "cypress,cy8c9540", .data = OF_CY8C95X(40), },
  76. { .compatible = "cypress,cy8c9560", .data = OF_CY8C95X(60), },
  77. { }
  78. };
  79. MODULE_DEVICE_TABLE(of, cy8c95x0_dt_ids);
  80. static const struct acpi_gpio_params cy8c95x0_irq_gpios = { 0, 0, true };
  81. static const struct acpi_gpio_mapping cy8c95x0_acpi_irq_gpios[] = {
  82. { "irq-gpios", &cy8c95x0_irq_gpios, 1, ACPI_GPIO_QUIRK_ABSOLUTE_NUMBER },
  83. { }
  84. };
  85. static int cy8c95x0_acpi_get_irq(struct device *dev)
  86. {
  87. int ret;
  88. ret = devm_acpi_dev_add_driver_gpios(dev, cy8c95x0_acpi_irq_gpios);
  89. if (ret)
  90. dev_warn(dev, "can't add GPIO ACPI mapping\n");
  91. ret = acpi_dev_gpio_irq_get_by(ACPI_COMPANION(dev), "irq", 0);
  92. if (ret < 0)
  93. return ret;
  94. dev_info(dev, "ACPI interrupt quirk (IRQ %d)\n", ret);
  95. return ret;
  96. }
  97. static const struct dmi_system_id cy8c95x0_dmi_acpi_irq_info[] = {
  98. {
  99. /*
  100. * On Intel Galileo Gen 1 board the IRQ pin is provided
  101. * as an absolute number instead of being relative.
  102. * Since first controller (gpio-sch.c) and second
  103. * (gpio-dwapb.c) are at the fixed bases, we may safely
  104. * refer to the number in the global space to get an IRQ
  105. * out of it.
  106. */
  107. .matches = {
  108. DMI_EXACT_MATCH(DMI_BOARD_NAME, "Galileo"),
  109. },
  110. },
  111. {}
  112. };
  113. /**
  114. * struct cy8c95x0_pinctrl - driver data
  115. * @regmap: Device's regmap. Only direct access registers.
  116. * @irq_lock: IRQ bus lock
  117. * @i2c_lock: Mutex to hold while using the regmap
  118. * @irq_mask: I/O bits affected by interrupts
  119. * @irq_trig_raise: I/O bits affected by raising voltage level
  120. * @irq_trig_fall: I/O bits affected by falling voltage level
  121. * @irq_trig_low: I/O bits affected by a low voltage level
  122. * @irq_trig_high: I/O bits affected by a high voltage level
  123. * @push_pull: I/O bits configured as push pull driver
  124. * @map: Mask used to compensate for Gport2 width
  125. * @nport: Number of Gports in this chip
  126. * @gpio_chip: gpiolib chip
  127. * @driver_data: private driver data
  128. * @dev: struct device
  129. * @pctldev: pin controller device
  130. * @pinctrl_desc: pin controller description
  131. * @name: Chip controller name
  132. * @tpin: Total number of pins
  133. * @gpio_reset: GPIO line handler that can reset the IC
  134. */
  135. struct cy8c95x0_pinctrl {
  136. struct regmap *regmap;
  137. struct mutex irq_lock;
  138. struct mutex i2c_lock;
  139. DECLARE_BITMAP(irq_mask, MAX_LINE);
  140. DECLARE_BITMAP(irq_trig_raise, MAX_LINE);
  141. DECLARE_BITMAP(irq_trig_fall, MAX_LINE);
  142. DECLARE_BITMAP(irq_trig_low, MAX_LINE);
  143. DECLARE_BITMAP(irq_trig_high, MAX_LINE);
  144. DECLARE_BITMAP(push_pull, MAX_LINE);
  145. DECLARE_BITMAP(map, MAX_LINE);
  146. unsigned int nport;
  147. struct gpio_chip gpio_chip;
  148. unsigned long driver_data;
  149. struct device *dev;
  150. struct pinctrl_dev *pctldev;
  151. struct pinctrl_desc pinctrl_desc;
  152. char name[32];
  153. unsigned int tpin;
  154. struct gpio_desc *gpio_reset;
  155. };
  156. static const struct pinctrl_pin_desc cy8c9560_pins[] = {
  157. PINCTRL_PIN(0, "gp00"),
  158. PINCTRL_PIN(1, "gp01"),
  159. PINCTRL_PIN(2, "gp02"),
  160. PINCTRL_PIN(3, "gp03"),
  161. PINCTRL_PIN(4, "gp04"),
  162. PINCTRL_PIN(5, "gp05"),
  163. PINCTRL_PIN(6, "gp06"),
  164. PINCTRL_PIN(7, "gp07"),
  165. PINCTRL_PIN(8, "gp10"),
  166. PINCTRL_PIN(9, "gp11"),
  167. PINCTRL_PIN(10, "gp12"),
  168. PINCTRL_PIN(11, "gp13"),
  169. PINCTRL_PIN(12, "gp14"),
  170. PINCTRL_PIN(13, "gp15"),
  171. PINCTRL_PIN(14, "gp16"),
  172. PINCTRL_PIN(15, "gp17"),
  173. PINCTRL_PIN(16, "gp20"),
  174. PINCTRL_PIN(17, "gp21"),
  175. PINCTRL_PIN(18, "gp22"),
  176. PINCTRL_PIN(19, "gp23"),
  177. PINCTRL_PIN(20, "gp30"),
  178. PINCTRL_PIN(21, "gp31"),
  179. PINCTRL_PIN(22, "gp32"),
  180. PINCTRL_PIN(23, "gp33"),
  181. PINCTRL_PIN(24, "gp34"),
  182. PINCTRL_PIN(25, "gp35"),
  183. PINCTRL_PIN(26, "gp36"),
  184. PINCTRL_PIN(27, "gp37"),
  185. PINCTRL_PIN(28, "gp40"),
  186. PINCTRL_PIN(29, "gp41"),
  187. PINCTRL_PIN(30, "gp42"),
  188. PINCTRL_PIN(31, "gp43"),
  189. PINCTRL_PIN(32, "gp44"),
  190. PINCTRL_PIN(33, "gp45"),
  191. PINCTRL_PIN(34, "gp46"),
  192. PINCTRL_PIN(35, "gp47"),
  193. PINCTRL_PIN(36, "gp50"),
  194. PINCTRL_PIN(37, "gp51"),
  195. PINCTRL_PIN(38, "gp52"),
  196. PINCTRL_PIN(39, "gp53"),
  197. PINCTRL_PIN(40, "gp54"),
  198. PINCTRL_PIN(41, "gp55"),
  199. PINCTRL_PIN(42, "gp56"),
  200. PINCTRL_PIN(43, "gp57"),
  201. PINCTRL_PIN(44, "gp60"),
  202. PINCTRL_PIN(45, "gp61"),
  203. PINCTRL_PIN(46, "gp62"),
  204. PINCTRL_PIN(47, "gp63"),
  205. PINCTRL_PIN(48, "gp64"),
  206. PINCTRL_PIN(49, "gp65"),
  207. PINCTRL_PIN(50, "gp66"),
  208. PINCTRL_PIN(51, "gp67"),
  209. PINCTRL_PIN(52, "gp70"),
  210. PINCTRL_PIN(53, "gp71"),
  211. PINCTRL_PIN(54, "gp72"),
  212. PINCTRL_PIN(55, "gp73"),
  213. PINCTRL_PIN(56, "gp74"),
  214. PINCTRL_PIN(57, "gp75"),
  215. PINCTRL_PIN(58, "gp76"),
  216. PINCTRL_PIN(59, "gp77"),
  217. };
  218. static const char * const cy8c95x0_groups[] = {
  219. "gp00",
  220. "gp01",
  221. "gp02",
  222. "gp03",
  223. "gp04",
  224. "gp05",
  225. "gp06",
  226. "gp07",
  227. "gp10",
  228. "gp11",
  229. "gp12",
  230. "gp13",
  231. "gp14",
  232. "gp15",
  233. "gp16",
  234. "gp17",
  235. "gp20",
  236. "gp21",
  237. "gp22",
  238. "gp23",
  239. "gp30",
  240. "gp31",
  241. "gp32",
  242. "gp33",
  243. "gp34",
  244. "gp35",
  245. "gp36",
  246. "gp37",
  247. "gp40",
  248. "gp41",
  249. "gp42",
  250. "gp43",
  251. "gp44",
  252. "gp45",
  253. "gp46",
  254. "gp47",
  255. "gp50",
  256. "gp51",
  257. "gp52",
  258. "gp53",
  259. "gp54",
  260. "gp55",
  261. "gp56",
  262. "gp57",
  263. "gp60",
  264. "gp61",
  265. "gp62",
  266. "gp63",
  267. "gp64",
  268. "gp65",
  269. "gp66",
  270. "gp67",
  271. "gp70",
  272. "gp71",
  273. "gp72",
  274. "gp73",
  275. "gp74",
  276. "gp75",
  277. "gp76",
  278. "gp77",
  279. };
  280. static inline u8 cypress_get_port(struct cy8c95x0_pinctrl *chip, unsigned int pin)
  281. {
  282. /* Account for GPORT2 which only has 4 bits */
  283. return CY8C95X0_PIN_TO_OFFSET(pin) / BANK_SZ;
  284. }
  285. static int cypress_get_pin_mask(struct cy8c95x0_pinctrl *chip, unsigned int pin)
  286. {
  287. /* Account for GPORT2 which only has 4 bits */
  288. return BIT(CY8C95X0_PIN_TO_OFFSET(pin) % BANK_SZ);
  289. }
  290. static bool cy8c95x0_readable_register(struct device *dev, unsigned int reg)
  291. {
  292. /*
  293. * Only 12 registers are present per port (see Table 6 in the datasheet).
  294. */
  295. if (reg >= CY8C95X0_VIRTUAL && (reg % MUXED_STRIDE) >= 12)
  296. return false;
  297. switch (reg) {
  298. case 0x24 ... 0x27:
  299. case 0x31 ... 0x3f:
  300. return false;
  301. default:
  302. return true;
  303. }
  304. }
  305. static bool cy8c95x0_writeable_register(struct device *dev, unsigned int reg)
  306. {
  307. /*
  308. * Only 12 registers are present per port (see Table 6 in the datasheet).
  309. */
  310. if (reg >= CY8C95X0_VIRTUAL && (reg % MUXED_STRIDE) >= 12)
  311. return false;
  312. switch (reg) {
  313. case CY8C95X0_INPUT_(0) ... CY8C95X0_INPUT_(7):
  314. return false;
  315. case CY8C95X0_DEVID:
  316. return false;
  317. case 0x24 ... 0x27:
  318. case 0x31 ... 0x3f:
  319. return false;
  320. default:
  321. return true;
  322. }
  323. }
  324. static bool cy8c95x0_volatile_register(struct device *dev, unsigned int reg)
  325. {
  326. switch (reg) {
  327. case CY8C95X0_INPUT_(0) ... CY8C95X0_INPUT_(7):
  328. case CY8C95X0_INTSTATUS_(0) ... CY8C95X0_INTSTATUS_(7):
  329. case CY8C95X0_INTMASK:
  330. case CY8C95X0_SELPWM:
  331. case CY8C95X0_INVERT:
  332. case CY8C95X0_DIRECTION:
  333. case CY8C95X0_DRV_PU:
  334. case CY8C95X0_DRV_PD:
  335. case CY8C95X0_DRV_ODH:
  336. case CY8C95X0_DRV_ODL:
  337. case CY8C95X0_DRV_PP_FAST:
  338. case CY8C95X0_DRV_PP_SLOW:
  339. case CY8C95X0_DRV_HIZ:
  340. return true;
  341. default:
  342. return false;
  343. }
  344. }
  345. static bool cy8c95x0_precious_register(struct device *dev, unsigned int reg)
  346. {
  347. switch (reg) {
  348. case CY8C95X0_INTSTATUS_(0) ... CY8C95X0_INTSTATUS_(7):
  349. return true;
  350. default:
  351. return false;
  352. }
  353. }
  354. static bool cy8c95x0_muxed_register(unsigned int reg)
  355. {
  356. switch (reg) {
  357. case CY8C95X0_INTMASK:
  358. case CY8C95X0_SELPWM:
  359. case CY8C95X0_INVERT:
  360. case CY8C95X0_DIRECTION:
  361. case CY8C95X0_DRV_PU:
  362. case CY8C95X0_DRV_PD:
  363. case CY8C95X0_DRV_ODH:
  364. case CY8C95X0_DRV_ODL:
  365. case CY8C95X0_DRV_PP_FAST:
  366. case CY8C95X0_DRV_PP_SLOW:
  367. case CY8C95X0_DRV_HIZ:
  368. return true;
  369. default:
  370. return false;
  371. }
  372. }
  373. static bool cy8c95x0_wc_register(unsigned int reg)
  374. {
  375. switch (reg) {
  376. case CY8C95X0_DRV_PU:
  377. case CY8C95X0_DRV_PD:
  378. case CY8C95X0_DRV_ODH:
  379. case CY8C95X0_DRV_ODL:
  380. case CY8C95X0_DRV_PP_FAST:
  381. case CY8C95X0_DRV_PP_SLOW:
  382. case CY8C95X0_DRV_HIZ:
  383. return true;
  384. default:
  385. return false;
  386. }
  387. }
  388. static bool cy8c95x0_quick_path_register(unsigned int reg)
  389. {
  390. switch (reg) {
  391. case CY8C95X0_INPUT_(0) ... CY8C95X0_INPUT_(7):
  392. case CY8C95X0_INTSTATUS_(0) ... CY8C95X0_INTSTATUS_(7):
  393. case CY8C95X0_OUTPUT_(0) ... CY8C95X0_OUTPUT_(7):
  394. return true;
  395. default:
  396. return false;
  397. }
  398. }
  399. static const struct regmap_range_cfg cy8c95x0_ranges[] = {
  400. {
  401. .range_min = CY8C95X0_VIRTUAL,
  402. .range_max = 0, /* Updated at runtime */
  403. .selector_reg = CY8C95X0_PORTSEL,
  404. .selector_mask = 0x07,
  405. .selector_shift = 0x0,
  406. .window_start = CY8C95X0_PORTSEL,
  407. .window_len = MUXED_STRIDE,
  408. }
  409. };
  410. static const struct regmap_config cy8c9520_i2c_regmap = {
  411. .reg_bits = 8,
  412. .val_bits = 8,
  413. .readable_reg = cy8c95x0_readable_register,
  414. .writeable_reg = cy8c95x0_writeable_register,
  415. .volatile_reg = cy8c95x0_volatile_register,
  416. .precious_reg = cy8c95x0_precious_register,
  417. .cache_type = REGCACHE_MAPLE,
  418. .ranges = NULL, /* Updated at runtime */
  419. .num_ranges = 1,
  420. .max_register = 0, /* Updated at runtime */
  421. .num_reg_defaults_raw = 0, /* Updated at runtime */
  422. .use_single_read = true, /* Workaround for regcache bug */
  423. #if IS_ENABLED(CONFIG_DEBUG_PINCTRL)
  424. .disable_locking = false,
  425. #else
  426. .disable_locking = true,
  427. #endif
  428. };
  429. /* Caller should never modify PORTSEL directly */
  430. static int cy8c95x0_regmap_update_bits_base(struct cy8c95x0_pinctrl *chip,
  431. unsigned int reg, unsigned int port,
  432. unsigned int mask, unsigned int val,
  433. bool *change, bool async, bool force)
  434. {
  435. int ret, off, i;
  436. /* Registers behind the PORTSEL mux have their own range in regmap */
  437. if (cy8c95x0_muxed_register(reg)) {
  438. off = CY8C95X0_MUX_REGMAP_TO_OFFSET(reg, port);
  439. } else {
  440. /* Quick path direct access registers honor the port argument */
  441. if (cy8c95x0_quick_path_register(reg))
  442. off = reg + port;
  443. else
  444. off = reg;
  445. }
  446. guard(mutex)(&chip->i2c_lock);
  447. ret = regmap_update_bits_base(chip->regmap, off, mask, val, change, async, force);
  448. if (ret < 0)
  449. return ret;
  450. /*
  451. * Mimic what hardware does and update the cache when a WC bit is written.
  452. * Allows to mark the registers as non-volatile and reduces I/O cycles.
  453. */
  454. if (cy8c95x0_wc_register(reg) && (mask & val)) {
  455. /* Writing a 1 clears set bits in the other drive mode registers */
  456. regcache_cache_only(chip->regmap, true);
  457. for (i = CY8C95X0_DRV_PU; i <= CY8C95X0_DRV_HIZ; i++) {
  458. if (i == reg)
  459. continue;
  460. off = CY8C95X0_MUX_REGMAP_TO_OFFSET(i, port);
  461. regmap_clear_bits(chip->regmap, off, mask & val);
  462. }
  463. regcache_cache_only(chip->regmap, false);
  464. }
  465. return 0;
  466. }
  467. /**
  468. * cy8c95x0_regmap_write_bits() - writes a register using the regmap cache
  469. * @chip: The pinctrl to work on
  470. * @reg: The register to write to. Can be direct access or muxed register.
  471. * MUST NOT be the PORTSEL register.
  472. * @port: The port to be used for muxed registers or quick path direct access
  473. * registers. Otherwise unused.
  474. * @mask: Bitmask to change
  475. * @val: New value for bitmask
  476. *
  477. * This function handles the register writes to the direct access registers and
  478. * the muxed registers while caching all register accesses, internally handling
  479. * the correct state of the PORTSEL register and protecting the access to muxed
  480. * registers.
  481. * The caller must only use this function to change registers behind the PORTSEL mux.
  482. *
  483. * Return: 0 for successful request, else a corresponding error value
  484. */
  485. static int cy8c95x0_regmap_write_bits(struct cy8c95x0_pinctrl *chip, unsigned int reg,
  486. unsigned int port, unsigned int mask, unsigned int val)
  487. {
  488. return cy8c95x0_regmap_update_bits_base(chip, reg, port, mask, val, NULL, false, true);
  489. }
  490. /**
  491. * cy8c95x0_regmap_update_bits() - updates a register using the regmap cache
  492. * @chip: The pinctrl to work on
  493. * @reg: The register to write to. Can be direct access or muxed register.
  494. * MUST NOT be the PORTSEL register.
  495. * @port: The port to be used for muxed registers or quick path direct access
  496. * registers. Otherwise unused.
  497. * @mask: Bitmask to change
  498. * @val: New value for bitmask
  499. *
  500. * This function handles the register updates to the direct access registers and
  501. * the muxed registers while caching all register accesses, internally handling
  502. * the correct state of the PORTSEL register and protecting the access to muxed
  503. * registers.
  504. * The caller must only use this function to change registers behind the PORTSEL mux.
  505. *
  506. * Return: 0 for successful request, else a corresponding error value
  507. */
  508. static int cy8c95x0_regmap_update_bits(struct cy8c95x0_pinctrl *chip, unsigned int reg,
  509. unsigned int port, unsigned int mask, unsigned int val)
  510. {
  511. return cy8c95x0_regmap_update_bits_base(chip, reg, port, mask, val, NULL, false, false);
  512. }
  513. /**
  514. * cy8c95x0_regmap_read_bits() - reads a register using the regmap cache
  515. * @chip: The pinctrl to work on
  516. * @reg: The register to read from. Can be direct access or muxed register.
  517. * @port: The port to be used for muxed registers or quick path direct access
  518. * registers. Otherwise unused.
  519. * @mask: Bitmask to apply
  520. * @val: Value read from hardware or cache
  521. *
  522. * This function handles the register reads from the direct access registers and
  523. * the muxed registers while caching all register accesses, internally handling
  524. * the correct state of the PORTSEL register and protecting the access to muxed
  525. * registers.
  526. * The caller must only use this function to read registers behind the PORTSEL mux.
  527. *
  528. * Return: 0 for successful request, else a corresponding error value
  529. */
  530. static int cy8c95x0_regmap_read_bits(struct cy8c95x0_pinctrl *chip, unsigned int reg,
  531. unsigned int port, unsigned int mask, unsigned int *val)
  532. {
  533. unsigned int off;
  534. unsigned int tmp;
  535. int ret;
  536. /* Registers behind the PORTSEL mux have their own range in regmap */
  537. if (cy8c95x0_muxed_register(reg)) {
  538. off = CY8C95X0_MUX_REGMAP_TO_OFFSET(reg, port);
  539. } else {
  540. /* Quick path direct access registers honor the port argument */
  541. if (cy8c95x0_quick_path_register(reg))
  542. off = reg + port;
  543. else
  544. off = reg;
  545. }
  546. scoped_guard(mutex, &chip->i2c_lock)
  547. ret = regmap_read(chip->regmap, off, &tmp);
  548. if (ret)
  549. return ret;
  550. *val = tmp & mask;
  551. return 0;
  552. }
  553. static int cy8c95x0_write_regs_mask(struct cy8c95x0_pinctrl *chip, int reg,
  554. unsigned long *val, unsigned long *mask)
  555. {
  556. DECLARE_BITMAP(tmask, MAX_LINE);
  557. DECLARE_BITMAP(tval, MAX_LINE);
  558. unsigned long bits, offset;
  559. int write_val;
  560. int ret;
  561. /* Add the 4 bit gap of Gport2 */
  562. bitmap_scatter(tmask, mask, chip->map, MAX_LINE);
  563. bitmap_scatter(tval, val, chip->map, MAX_LINE);
  564. for_each_set_clump8(offset, bits, tmask, chip->nport * BANK_SZ) {
  565. unsigned int i = offset / 8;
  566. write_val = bitmap_get_value8(tval, offset);
  567. ret = cy8c95x0_regmap_update_bits(chip, reg, i, bits, write_val);
  568. if (ret < 0) {
  569. dev_err(chip->dev, "failed writing register %d, port %u: err %d\n", reg, i, ret);
  570. return ret;
  571. }
  572. }
  573. return 0;
  574. }
  575. static int cy8c95x0_read_regs_mask(struct cy8c95x0_pinctrl *chip, int reg,
  576. unsigned long *val, unsigned long *mask)
  577. {
  578. DECLARE_BITMAP(tmask, MAX_LINE);
  579. DECLARE_BITMAP(tval, MAX_LINE);
  580. unsigned long bits, offset;
  581. unsigned int read_val;
  582. int ret;
  583. /* Add the 4 bit gap of Gport2 */
  584. bitmap_scatter(tmask, mask, chip->map, MAX_LINE);
  585. bitmap_scatter(tval, val, chip->map, MAX_LINE);
  586. for_each_set_clump8(offset, bits, tmask, chip->nport * BANK_SZ) {
  587. unsigned int i = offset / 8;
  588. ret = cy8c95x0_regmap_read_bits(chip, reg, i, bits, &read_val);
  589. if (ret < 0) {
  590. dev_err(chip->dev, "failed reading register %d, port %u: err %d\n", reg, i, ret);
  591. return ret;
  592. }
  593. read_val |= bitmap_get_value8(tval, offset) & ~bits;
  594. bitmap_set_value8(tval, read_val, offset);
  595. }
  596. /* Fill the 4 bit gap of Gport2 */
  597. bitmap_gather(val, tval, chip->map, MAX_LINE);
  598. return 0;
  599. }
  600. static int cy8c95x0_pinmux_direction(struct cy8c95x0_pinctrl *chip, unsigned int pin, bool input)
  601. {
  602. u8 port = cypress_get_port(chip, pin);
  603. u8 bit = cypress_get_pin_mask(chip, pin);
  604. int ret;
  605. ret = cy8c95x0_regmap_write_bits(chip, CY8C95X0_DIRECTION, port, bit, input ? bit : 0);
  606. if (ret)
  607. return ret;
  608. /*
  609. * Disable driving the pin by forcing it to HighZ. Only setting
  610. * the direction register isn't sufficient in Push-Pull mode.
  611. */
  612. if (input && test_bit(pin, chip->push_pull)) {
  613. ret = cy8c95x0_regmap_write_bits(chip, CY8C95X0_DRV_HIZ, port, bit, bit);
  614. if (ret)
  615. return ret;
  616. __clear_bit(pin, chip->push_pull);
  617. }
  618. return 0;
  619. }
  620. static int cy8c95x0_gpio_direction_input(struct gpio_chip *gc, unsigned int off)
  621. {
  622. return pinctrl_gpio_direction_input(gc, off);
  623. }
  624. static int cy8c95x0_gpio_direction_output(struct gpio_chip *gc,
  625. unsigned int off, int val)
  626. {
  627. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  628. u8 port = cypress_get_port(chip, off);
  629. u8 bit = cypress_get_pin_mask(chip, off);
  630. int ret;
  631. /* Set output level */
  632. ret = cy8c95x0_regmap_write_bits(chip, CY8C95X0_OUTPUT, port, bit, val ? bit : 0);
  633. if (ret)
  634. return ret;
  635. return pinctrl_gpio_direction_output(gc, off);
  636. }
  637. static int cy8c95x0_gpio_get_value(struct gpio_chip *gc, unsigned int off)
  638. {
  639. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  640. u8 port = cypress_get_port(chip, off);
  641. u8 bit = cypress_get_pin_mask(chip, off);
  642. unsigned int reg_val;
  643. int ret;
  644. ret = cy8c95x0_regmap_read_bits(chip, CY8C95X0_INPUT, port, bit, &reg_val);
  645. if (ret < 0) {
  646. /*
  647. * NOTE:
  648. * Diagnostic already emitted; that's all we should
  649. * do unless gpio_*_value_cansleep() calls become different
  650. * from their nonsleeping siblings (and report faults).
  651. */
  652. return 0;
  653. }
  654. return reg_val ? 1 : 0;
  655. }
  656. static int cy8c95x0_gpio_set_value(struct gpio_chip *gc, unsigned int off,
  657. int val)
  658. {
  659. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  660. u8 port = cypress_get_port(chip, off);
  661. u8 bit = cypress_get_pin_mask(chip, off);
  662. return cy8c95x0_regmap_write_bits(chip, CY8C95X0_OUTPUT, port, bit,
  663. val ? bit : 0);
  664. }
  665. static int cy8c95x0_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
  666. {
  667. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  668. u8 port = cypress_get_port(chip, off);
  669. u8 bit = cypress_get_pin_mask(chip, off);
  670. unsigned int reg_val;
  671. int ret;
  672. ret = cy8c95x0_regmap_read_bits(chip, CY8C95X0_DIRECTION, port, bit, &reg_val);
  673. if (ret < 0)
  674. return ret;
  675. if (reg_val)
  676. return GPIO_LINE_DIRECTION_IN;
  677. return GPIO_LINE_DIRECTION_OUT;
  678. }
  679. static int cy8c95x0_gpio_get_pincfg(struct cy8c95x0_pinctrl *chip,
  680. unsigned int off,
  681. unsigned long *config)
  682. {
  683. enum pin_config_param param = pinconf_to_config_param(*config);
  684. u8 port = cypress_get_port(chip, off);
  685. u8 bit = cypress_get_pin_mask(chip, off);
  686. unsigned int reg_val;
  687. unsigned int reg;
  688. u16 arg = 0;
  689. int ret;
  690. switch (param) {
  691. case PIN_CONFIG_BIAS_PULL_UP:
  692. reg = CY8C95X0_DRV_PU;
  693. break;
  694. case PIN_CONFIG_BIAS_PULL_DOWN:
  695. reg = CY8C95X0_DRV_PD;
  696. break;
  697. case PIN_CONFIG_BIAS_DISABLE:
  698. reg = CY8C95X0_DRV_HIZ;
  699. break;
  700. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  701. reg = CY8C95X0_DRV_ODL;
  702. break;
  703. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  704. reg = CY8C95X0_DRV_ODH;
  705. break;
  706. case PIN_CONFIG_DRIVE_PUSH_PULL:
  707. reg = CY8C95X0_DRV_PP_FAST;
  708. break;
  709. case PIN_CONFIG_INPUT_ENABLE:
  710. reg = CY8C95X0_DIRECTION;
  711. break;
  712. case PIN_CONFIG_MODE_PWM:
  713. reg = CY8C95X0_SELPWM;
  714. break;
  715. case PIN_CONFIG_LEVEL:
  716. reg = CY8C95X0_OUTPUT;
  717. break;
  718. case PIN_CONFIG_OUTPUT_ENABLE:
  719. reg = CY8C95X0_DIRECTION;
  720. break;
  721. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  722. case PIN_CONFIG_BIAS_BUS_HOLD:
  723. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  724. case PIN_CONFIG_DRIVE_STRENGTH:
  725. case PIN_CONFIG_DRIVE_STRENGTH_UA:
  726. case PIN_CONFIG_INPUT_DEBOUNCE:
  727. case PIN_CONFIG_INPUT_SCHMITT:
  728. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  729. case PIN_CONFIG_MODE_LOW_POWER:
  730. case PIN_CONFIG_PERSIST_STATE:
  731. case PIN_CONFIG_POWER_SOURCE:
  732. case PIN_CONFIG_SKEW_DELAY:
  733. case PIN_CONFIG_SLEEP_HARDWARE_STATE:
  734. case PIN_CONFIG_SLEW_RATE:
  735. default:
  736. return -ENOTSUPP;
  737. }
  738. /*
  739. * Writing 1 to one of the drive mode registers will automatically
  740. * clear conflicting set bits in the other drive mode registers.
  741. */
  742. ret = cy8c95x0_regmap_read_bits(chip, reg, port, bit, &reg_val);
  743. if (ret < 0)
  744. return ret;
  745. if (reg_val)
  746. arg = 1;
  747. if (param == PIN_CONFIG_OUTPUT_ENABLE)
  748. arg = !arg;
  749. *config = pinconf_to_config_packed(param, arg);
  750. return 0;
  751. }
  752. static int cy8c95x0_gpio_set_pincfg(struct cy8c95x0_pinctrl *chip,
  753. unsigned int off,
  754. unsigned long config)
  755. {
  756. u8 port = cypress_get_port(chip, off);
  757. u8 bit = cypress_get_pin_mask(chip, off);
  758. unsigned long param = pinconf_to_config_param(config);
  759. unsigned long arg = pinconf_to_config_argument(config);
  760. unsigned int reg;
  761. switch (param) {
  762. case PIN_CONFIG_BIAS_PULL_UP:
  763. __clear_bit(off, chip->push_pull);
  764. reg = CY8C95X0_DRV_PU;
  765. break;
  766. case PIN_CONFIG_BIAS_PULL_DOWN:
  767. __clear_bit(off, chip->push_pull);
  768. reg = CY8C95X0_DRV_PD;
  769. break;
  770. case PIN_CONFIG_BIAS_DISABLE:
  771. __clear_bit(off, chip->push_pull);
  772. reg = CY8C95X0_DRV_HIZ;
  773. break;
  774. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  775. __clear_bit(off, chip->push_pull);
  776. reg = CY8C95X0_DRV_ODL;
  777. break;
  778. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  779. __clear_bit(off, chip->push_pull);
  780. reg = CY8C95X0_DRV_ODH;
  781. break;
  782. case PIN_CONFIG_DRIVE_PUSH_PULL:
  783. __set_bit(off, chip->push_pull);
  784. reg = CY8C95X0_DRV_PP_FAST;
  785. break;
  786. case PIN_CONFIG_MODE_PWM:
  787. reg = CY8C95X0_SELPWM;
  788. break;
  789. case PIN_CONFIG_OUTPUT_ENABLE:
  790. return cy8c95x0_pinmux_direction(chip, off, !arg);
  791. case PIN_CONFIG_INPUT_ENABLE:
  792. return cy8c95x0_pinmux_direction(chip, off, arg);
  793. default:
  794. return -ENOTSUPP;
  795. }
  796. /*
  797. * Writing 1 to one of the drive mode registers will automatically
  798. * clear conflicting set bits in the other drive mode registers.
  799. */
  800. return cy8c95x0_regmap_write_bits(chip, reg, port, bit, bit);
  801. }
  802. static int cy8c95x0_gpio_get_multiple(struct gpio_chip *gc,
  803. unsigned long *mask, unsigned long *bits)
  804. {
  805. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  806. return cy8c95x0_read_regs_mask(chip, CY8C95X0_INPUT, bits, mask);
  807. }
  808. static int cy8c95x0_gpio_set_multiple(struct gpio_chip *gc,
  809. unsigned long *mask, unsigned long *bits)
  810. {
  811. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  812. return cy8c95x0_write_regs_mask(chip, CY8C95X0_OUTPUT, bits, mask);
  813. }
  814. static int cy8c95x0_add_pin_ranges(struct gpio_chip *gc)
  815. {
  816. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  817. struct device *dev = chip->dev;
  818. int ret;
  819. ret = gpiochip_add_pin_range(gc, dev_name(dev), 0, 0, chip->tpin);
  820. if (ret)
  821. dev_err(dev, "failed to add GPIO pin range\n");
  822. return ret;
  823. }
  824. static int cy8c95x0_setup_gpiochip(struct cy8c95x0_pinctrl *chip)
  825. {
  826. struct gpio_chip *gc = &chip->gpio_chip;
  827. gc->request = gpiochip_generic_request;
  828. gc->free = gpiochip_generic_free;
  829. gc->direction_input = cy8c95x0_gpio_direction_input;
  830. gc->direction_output = cy8c95x0_gpio_direction_output;
  831. gc->get = cy8c95x0_gpio_get_value;
  832. gc->set = cy8c95x0_gpio_set_value;
  833. gc->get_direction = cy8c95x0_gpio_get_direction;
  834. gc->get_multiple = cy8c95x0_gpio_get_multiple;
  835. gc->set_multiple = cy8c95x0_gpio_set_multiple;
  836. gc->set_config = gpiochip_generic_config;
  837. gc->can_sleep = true;
  838. gc->add_pin_ranges = cy8c95x0_add_pin_ranges;
  839. gc->base = -1;
  840. gc->ngpio = chip->tpin;
  841. gc->parent = chip->dev;
  842. gc->owner = THIS_MODULE;
  843. gc->names = NULL;
  844. gc->label = dev_name(chip->dev);
  845. return devm_gpiochip_add_data(chip->dev, gc, chip);
  846. }
  847. static void cy8c95x0_irq_mask(struct irq_data *d)
  848. {
  849. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  850. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  851. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  852. set_bit(hwirq, chip->irq_mask);
  853. gpiochip_disable_irq(gc, hwirq);
  854. }
  855. static void cy8c95x0_irq_unmask(struct irq_data *d)
  856. {
  857. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  858. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  859. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  860. gpiochip_enable_irq(gc, hwirq);
  861. clear_bit(hwirq, chip->irq_mask);
  862. }
  863. static void cy8c95x0_irq_bus_lock(struct irq_data *d)
  864. {
  865. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  866. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  867. mutex_lock(&chip->irq_lock);
  868. }
  869. static void cy8c95x0_irq_bus_sync_unlock(struct irq_data *d)
  870. {
  871. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  872. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  873. DECLARE_BITMAP(ones, MAX_LINE);
  874. DECLARE_BITMAP(irq_mask, MAX_LINE);
  875. DECLARE_BITMAP(reg_direction, MAX_LINE);
  876. bitmap_fill(ones, MAX_LINE);
  877. cy8c95x0_write_regs_mask(chip, CY8C95X0_INTMASK, chip->irq_mask, ones);
  878. /* Switch direction to input if needed */
  879. cy8c95x0_read_regs_mask(chip, CY8C95X0_DIRECTION, reg_direction, chip->irq_mask);
  880. bitmap_or(irq_mask, chip->irq_mask, reg_direction, MAX_LINE);
  881. bitmap_complement(irq_mask, irq_mask, MAX_LINE);
  882. /* Look for any newly setup interrupt */
  883. cy8c95x0_write_regs_mask(chip, CY8C95X0_DIRECTION, ones, irq_mask);
  884. mutex_unlock(&chip->irq_lock);
  885. }
  886. static int cy8c95x0_irq_set_type(struct irq_data *d, unsigned int type)
  887. {
  888. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  889. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  890. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  891. unsigned int trig_type;
  892. switch (type) {
  893. case IRQ_TYPE_EDGE_RISING:
  894. case IRQ_TYPE_EDGE_FALLING:
  895. case IRQ_TYPE_EDGE_BOTH:
  896. trig_type = type;
  897. break;
  898. case IRQ_TYPE_LEVEL_HIGH:
  899. trig_type = IRQ_TYPE_EDGE_RISING;
  900. break;
  901. case IRQ_TYPE_LEVEL_LOW:
  902. trig_type = IRQ_TYPE_EDGE_FALLING;
  903. break;
  904. default:
  905. dev_err(chip->dev, "irq %d: unsupported type %d\n", d->irq, type);
  906. return -EINVAL;
  907. }
  908. assign_bit(hwirq, chip->irq_trig_fall, trig_type & IRQ_TYPE_EDGE_FALLING);
  909. assign_bit(hwirq, chip->irq_trig_raise, trig_type & IRQ_TYPE_EDGE_RISING);
  910. assign_bit(hwirq, chip->irq_trig_low, type == IRQ_TYPE_LEVEL_LOW);
  911. assign_bit(hwirq, chip->irq_trig_high, type == IRQ_TYPE_LEVEL_HIGH);
  912. return 0;
  913. }
  914. static void cy8c95x0_irq_shutdown(struct irq_data *d)
  915. {
  916. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  917. struct cy8c95x0_pinctrl *chip = gpiochip_get_data(gc);
  918. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  919. clear_bit(hwirq, chip->irq_trig_raise);
  920. clear_bit(hwirq, chip->irq_trig_fall);
  921. clear_bit(hwirq, chip->irq_trig_low);
  922. clear_bit(hwirq, chip->irq_trig_high);
  923. }
  924. static const struct irq_chip cy8c95x0_irqchip = {
  925. .name = "cy8c95x0-irq",
  926. .irq_mask = cy8c95x0_irq_mask,
  927. .irq_unmask = cy8c95x0_irq_unmask,
  928. .irq_bus_lock = cy8c95x0_irq_bus_lock,
  929. .irq_bus_sync_unlock = cy8c95x0_irq_bus_sync_unlock,
  930. .irq_set_type = cy8c95x0_irq_set_type,
  931. .irq_shutdown = cy8c95x0_irq_shutdown,
  932. .flags = IRQCHIP_IMMUTABLE,
  933. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  934. };
  935. static bool cy8c95x0_irq_pending(struct cy8c95x0_pinctrl *chip, unsigned long *pending)
  936. {
  937. DECLARE_BITMAP(ones, MAX_LINE);
  938. DECLARE_BITMAP(cur_stat, MAX_LINE);
  939. DECLARE_BITMAP(new_stat, MAX_LINE);
  940. DECLARE_BITMAP(trigger, MAX_LINE);
  941. bitmap_fill(ones, MAX_LINE);
  942. /* Read the current interrupt status from the device */
  943. if (cy8c95x0_read_regs_mask(chip, CY8C95X0_INTSTATUS, trigger, ones))
  944. return false;
  945. /* Check latched inputs */
  946. if (cy8c95x0_read_regs_mask(chip, CY8C95X0_INPUT, cur_stat, trigger))
  947. return false;
  948. /* Apply filter for rising/falling edge selection */
  949. bitmap_replace(new_stat, chip->irq_trig_fall, chip->irq_trig_raise,
  950. cur_stat, MAX_LINE);
  951. bitmap_and(pending, new_stat, trigger, MAX_LINE);
  952. return !bitmap_empty(pending, MAX_LINE);
  953. }
  954. static irqreturn_t cy8c95x0_irq_handler(int irq, void *devid)
  955. {
  956. struct cy8c95x0_pinctrl *chip = devid;
  957. struct gpio_chip *gc = &chip->gpio_chip;
  958. DECLARE_BITMAP(pending, MAX_LINE);
  959. int nested_irq, level;
  960. bool ret;
  961. ret = cy8c95x0_irq_pending(chip, pending);
  962. if (!ret)
  963. return IRQ_RETVAL(0);
  964. ret = false;
  965. for_each_set_bit(level, pending, MAX_LINE) {
  966. /* Already accounted for 4bit gap in GPort2 */
  967. nested_irq = irq_find_mapping(gc->irq.domain, level);
  968. if (unlikely(nested_irq <= 0)) {
  969. dev_warn_ratelimited(gc->parent, "unmapped interrupt %d\n", level);
  970. continue;
  971. }
  972. if (test_bit(level, chip->irq_trig_low))
  973. while (!cy8c95x0_gpio_get_value(gc, level))
  974. handle_nested_irq(nested_irq);
  975. else if (test_bit(level, chip->irq_trig_high))
  976. while (cy8c95x0_gpio_get_value(gc, level))
  977. handle_nested_irq(nested_irq);
  978. else
  979. handle_nested_irq(nested_irq);
  980. ret = true;
  981. }
  982. return IRQ_RETVAL(ret);
  983. }
  984. static int cy8c95x0_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  985. {
  986. struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
  987. return chip->tpin;
  988. }
  989. static const char *cy8c95x0_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  990. unsigned int group)
  991. {
  992. return cy8c95x0_groups[group];
  993. }
  994. static int cy8c95x0_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  995. unsigned int group,
  996. const unsigned int **pins,
  997. unsigned int *num_pins)
  998. {
  999. *pins = &cy8c9560_pins[group].number;
  1000. *num_pins = 1;
  1001. return 0;
  1002. }
  1003. static const char *cy8c95x0_get_fname(unsigned int selector)
  1004. {
  1005. if (selector == 0)
  1006. return "gpio";
  1007. else
  1008. return "pwm";
  1009. }
  1010. static void cy8c95x0_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  1011. unsigned int pin)
  1012. {
  1013. struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
  1014. DECLARE_BITMAP(mask, MAX_LINE);
  1015. DECLARE_BITMAP(pwm, MAX_LINE);
  1016. bitmap_zero(mask, MAX_LINE);
  1017. __set_bit(pin, mask);
  1018. if (cy8c95x0_read_regs_mask(chip, CY8C95X0_SELPWM, pwm, mask)) {
  1019. seq_puts(s, "not available");
  1020. return;
  1021. }
  1022. seq_printf(s, "MODE:%s", cy8c95x0_get_fname(test_bit(pin, pwm)));
  1023. }
  1024. static const struct pinctrl_ops cy8c95x0_pinctrl_ops = {
  1025. .get_groups_count = cy8c95x0_pinctrl_get_groups_count,
  1026. .get_group_name = cy8c95x0_pinctrl_get_group_name,
  1027. .get_group_pins = cy8c95x0_pinctrl_get_group_pins,
  1028. #ifdef CONFIG_OF
  1029. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  1030. .dt_free_map = pinconf_generic_dt_free_map,
  1031. #endif
  1032. .pin_dbg_show = cy8c95x0_pin_dbg_show,
  1033. };
  1034. static const char *cy8c95x0_get_function_name(struct pinctrl_dev *pctldev, unsigned int selector)
  1035. {
  1036. return cy8c95x0_get_fname(selector);
  1037. }
  1038. static int cy8c95x0_get_functions_count(struct pinctrl_dev *pctldev)
  1039. {
  1040. return 2;
  1041. }
  1042. static int cy8c95x0_get_function_groups(struct pinctrl_dev *pctldev, unsigned int selector,
  1043. const char * const **groups,
  1044. unsigned int * const num_groups)
  1045. {
  1046. struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
  1047. *groups = cy8c95x0_groups;
  1048. *num_groups = chip->tpin;
  1049. return 0;
  1050. }
  1051. static int cy8c95x0_set_mode(struct cy8c95x0_pinctrl *chip, unsigned int off, bool mode)
  1052. {
  1053. u8 port = cypress_get_port(chip, off);
  1054. u8 bit = cypress_get_pin_mask(chip, off);
  1055. return cy8c95x0_regmap_write_bits(chip, CY8C95X0_SELPWM, port, bit, mode ? bit : 0);
  1056. }
  1057. static int cy8c95x0_pinmux_mode(struct cy8c95x0_pinctrl *chip,
  1058. unsigned int selector, unsigned int group)
  1059. {
  1060. u8 port = cypress_get_port(chip, group);
  1061. u8 bit = cypress_get_pin_mask(chip, group);
  1062. int ret;
  1063. ret = cy8c95x0_set_mode(chip, group, selector);
  1064. if (ret < 0)
  1065. return ret;
  1066. if (selector == 0)
  1067. return 0;
  1068. /* Set direction to output & set output to 1 so that PWM can work */
  1069. ret = cy8c95x0_regmap_write_bits(chip, CY8C95X0_DIRECTION, port, bit, bit);
  1070. if (ret < 0)
  1071. return ret;
  1072. return cy8c95x0_regmap_write_bits(chip, CY8C95X0_OUTPUT, port, bit, bit);
  1073. }
  1074. static int cy8c95x0_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
  1075. unsigned int group)
  1076. {
  1077. struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
  1078. return cy8c95x0_pinmux_mode(chip, selector, group);
  1079. }
  1080. static int cy8c95x0_gpio_request_enable(struct pinctrl_dev *pctldev,
  1081. struct pinctrl_gpio_range *range,
  1082. unsigned int pin)
  1083. {
  1084. struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
  1085. return cy8c95x0_set_mode(chip, pin, false);
  1086. }
  1087. static int cy8c95x0_gpio_set_direction(struct pinctrl_dev *pctldev,
  1088. struct pinctrl_gpio_range *range,
  1089. unsigned int pin, bool input)
  1090. {
  1091. struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
  1092. return cy8c95x0_pinmux_direction(chip, pin, input);
  1093. }
  1094. static const struct pinmux_ops cy8c95x0_pmxops = {
  1095. .get_functions_count = cy8c95x0_get_functions_count,
  1096. .get_function_name = cy8c95x0_get_function_name,
  1097. .get_function_groups = cy8c95x0_get_function_groups,
  1098. .set_mux = cy8c95x0_set_mux,
  1099. .gpio_request_enable = cy8c95x0_gpio_request_enable,
  1100. .gpio_set_direction = cy8c95x0_gpio_set_direction,
  1101. .strict = true,
  1102. };
  1103. static int cy8c95x0_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  1104. unsigned long *config)
  1105. {
  1106. struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
  1107. return cy8c95x0_gpio_get_pincfg(chip, pin, config);
  1108. }
  1109. static int cy8c95x0_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  1110. unsigned long *configs, unsigned int num_configs)
  1111. {
  1112. struct cy8c95x0_pinctrl *chip = pinctrl_dev_get_drvdata(pctldev);
  1113. int ret;
  1114. int i;
  1115. for (i = 0; i < num_configs; i++) {
  1116. ret = cy8c95x0_gpio_set_pincfg(chip, pin, configs[i]);
  1117. if (ret)
  1118. return ret;
  1119. }
  1120. return 0;
  1121. }
  1122. static const struct pinconf_ops cy8c95x0_pinconf_ops = {
  1123. .pin_config_get = cy8c95x0_pinconf_get,
  1124. .pin_config_set = cy8c95x0_pinconf_set,
  1125. .is_generic = true,
  1126. };
  1127. static int cy8c95x0_irq_setup(struct cy8c95x0_pinctrl *chip, int irq)
  1128. {
  1129. struct gpio_irq_chip *girq = &chip->gpio_chip.irq;
  1130. DECLARE_BITMAP(pending_irqs, MAX_LINE);
  1131. int ret;
  1132. mutex_init(&chip->irq_lock);
  1133. bitmap_zero(pending_irqs, MAX_LINE);
  1134. /* Read IRQ status register to clear all pending interrupts */
  1135. ret = cy8c95x0_irq_pending(chip, pending_irqs);
  1136. if (ret) {
  1137. dev_err(chip->dev, "failed to clear irq status register\n");
  1138. return ret;
  1139. }
  1140. /* Mask all interrupts */
  1141. bitmap_fill(chip->irq_mask, MAX_LINE);
  1142. gpio_irq_chip_set_chip(girq, &cy8c95x0_irqchip);
  1143. /* This will let us handle the parent IRQ in the driver */
  1144. girq->parent_handler = NULL;
  1145. girq->num_parents = 0;
  1146. girq->parents = NULL;
  1147. girq->default_type = IRQ_TYPE_NONE;
  1148. girq->handler = handle_simple_irq;
  1149. girq->threaded = true;
  1150. ret = devm_request_threaded_irq(chip->dev, irq,
  1151. NULL, cy8c95x0_irq_handler,
  1152. IRQF_ONESHOT | IRQF_SHARED,
  1153. dev_name(chip->dev), chip);
  1154. if (ret) {
  1155. dev_err(chip->dev, "failed to request irq %d\n", irq);
  1156. return ret;
  1157. }
  1158. dev_info(chip->dev, "Registered threaded IRQ\n");
  1159. return 0;
  1160. }
  1161. static int cy8c95x0_setup_pinctrl(struct cy8c95x0_pinctrl *chip)
  1162. {
  1163. struct pinctrl_desc *pd = &chip->pinctrl_desc;
  1164. pd->pctlops = &cy8c95x0_pinctrl_ops;
  1165. pd->confops = &cy8c95x0_pinconf_ops;
  1166. pd->pmxops = &cy8c95x0_pmxops;
  1167. pd->name = dev_name(chip->dev);
  1168. pd->pins = cy8c9560_pins;
  1169. pd->npins = chip->tpin;
  1170. pd->owner = THIS_MODULE;
  1171. chip->pctldev = devm_pinctrl_register(chip->dev, pd, chip);
  1172. if (IS_ERR(chip->pctldev))
  1173. return dev_err_probe(chip->dev, PTR_ERR(chip->pctldev),
  1174. "can't register controller\n");
  1175. return 0;
  1176. }
  1177. static int cy8c95x0_detect(struct i2c_client *client,
  1178. struct i2c_board_info *info)
  1179. {
  1180. struct i2c_adapter *adapter = client->adapter;
  1181. int ret;
  1182. const char *name;
  1183. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1184. return -ENODEV;
  1185. ret = i2c_smbus_read_byte_data(client, CY8C95X0_DEVID);
  1186. if (ret < 0)
  1187. return ret;
  1188. switch (ret & GENMASK(7, 4)) {
  1189. case 0x20:
  1190. name = cy8c95x0_id[0].name;
  1191. break;
  1192. case 0x40:
  1193. name = cy8c95x0_id[1].name;
  1194. break;
  1195. case 0x60:
  1196. name = cy8c95x0_id[2].name;
  1197. break;
  1198. default:
  1199. return -ENODEV;
  1200. }
  1201. dev_info(&client->dev, "Found a %s chip at 0x%02x.\n", name, client->addr);
  1202. strscpy(info->type, name);
  1203. return 0;
  1204. }
  1205. static int cy8c95x0_probe(struct i2c_client *client)
  1206. {
  1207. struct device *dev = &client->dev;
  1208. struct cy8c95x0_pinctrl *chip;
  1209. struct regmap_config regmap_conf;
  1210. struct regmap_range_cfg regmap_range_conf;
  1211. int ret;
  1212. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  1213. if (!chip)
  1214. return -ENOMEM;
  1215. chip->dev = dev;
  1216. /* Set the device type */
  1217. chip->driver_data = (uintptr_t)i2c_get_match_data(client);
  1218. if (!chip->driver_data)
  1219. return -ENODEV;
  1220. chip->tpin = chip->driver_data & CY8C95X0_GPIO_MASK;
  1221. chip->nport = DIV_ROUND_UP(CY8C95X0_PIN_TO_OFFSET(chip->tpin), BANK_SZ);
  1222. memcpy(&regmap_range_conf, &cy8c95x0_ranges[0], sizeof(regmap_range_conf));
  1223. switch (chip->tpin) {
  1224. case 20:
  1225. strscpy(chip->name, cy8c95x0_id[0].name);
  1226. regmap_range_conf.range_max = CY8C95X0_VIRTUAL + 3 * MUXED_STRIDE - 1;
  1227. break;
  1228. case 40:
  1229. strscpy(chip->name, cy8c95x0_id[1].name);
  1230. regmap_range_conf.range_max = CY8C95X0_VIRTUAL + 6 * MUXED_STRIDE - 1;
  1231. break;
  1232. case 60:
  1233. strscpy(chip->name, cy8c95x0_id[2].name);
  1234. regmap_range_conf.range_max = CY8C95X0_VIRTUAL + 8 * MUXED_STRIDE - 1;
  1235. break;
  1236. default:
  1237. return -ENODEV;
  1238. }
  1239. ret = devm_regulator_get_enable(dev, "vdd");
  1240. if (ret)
  1241. return dev_err_probe(dev, ret, "failed to enable regulator vdd\n");
  1242. /* bring the chip out of reset if reset pin is provided */
  1243. chip->gpio_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  1244. if (IS_ERR(chip->gpio_reset))
  1245. return dev_err_probe(dev, PTR_ERR(chip->gpio_reset), "Failed to get GPIO 'reset'\n");
  1246. gpiod_set_consumer_name(chip->gpio_reset, "CY8C95X0 RESET");
  1247. if (chip->gpio_reset) {
  1248. fsleep(1000);
  1249. gpiod_set_value_cansleep(chip->gpio_reset, 0);
  1250. fsleep(250000);
  1251. }
  1252. /* Regmap for direct and paged registers */
  1253. memcpy(&regmap_conf, &cy8c9520_i2c_regmap, sizeof(regmap_conf));
  1254. regmap_conf.ranges = &regmap_range_conf;
  1255. regmap_conf.max_register = regmap_range_conf.range_max;
  1256. regmap_conf.num_reg_defaults_raw = regmap_range_conf.range_max;
  1257. chip->regmap = devm_regmap_init_i2c(client, &regmap_conf);
  1258. if (IS_ERR(chip->regmap))
  1259. return PTR_ERR(chip->regmap);
  1260. bitmap_zero(chip->push_pull, MAX_LINE);
  1261. /* Setup HW pins mapping */
  1262. bitmap_fill(chip->map, MAX_LINE);
  1263. bitmap_clear(chip->map, 20, 4);
  1264. mutex_init(&chip->i2c_lock);
  1265. if (dmi_first_match(cy8c95x0_dmi_acpi_irq_info)) {
  1266. ret = cy8c95x0_acpi_get_irq(&client->dev);
  1267. if (ret > 0)
  1268. client->irq = ret;
  1269. }
  1270. if (client->irq) {
  1271. ret = cy8c95x0_irq_setup(chip, client->irq);
  1272. if (ret)
  1273. return ret;
  1274. }
  1275. ret = cy8c95x0_setup_pinctrl(chip);
  1276. if (ret)
  1277. return ret;
  1278. return cy8c95x0_setup_gpiochip(chip);
  1279. }
  1280. static const struct acpi_device_id cy8c95x0_acpi_ids[] = {
  1281. { "INT3490", 40, },
  1282. { }
  1283. };
  1284. MODULE_DEVICE_TABLE(acpi, cy8c95x0_acpi_ids);
  1285. static struct i2c_driver cy8c95x0_driver = {
  1286. .driver = {
  1287. .name = "cy8c95x0-pinctrl",
  1288. .of_match_table = cy8c95x0_dt_ids,
  1289. .acpi_match_table = cy8c95x0_acpi_ids,
  1290. },
  1291. .probe = cy8c95x0_probe,
  1292. .id_table = cy8c95x0_id,
  1293. .detect = cy8c95x0_detect,
  1294. };
  1295. module_i2c_driver(cy8c95x0_driver);
  1296. MODULE_AUTHOR("Patrick Rudolph <patrick.rudolph@9elements.com>");
  1297. MODULE_AUTHOR("Naresh Solanki <naresh.solanki@9elements.com>");
  1298. MODULE_DESCRIPTION("Pinctrl driver for CY8C95X0");
  1299. MODULE_LICENSE("GPL");