pinctrl-cy8c95x0.c 41 KB

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