pinctrl-cy8c95x0.c 35 KB

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