gpio-winbond.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * GPIO interface for Winbond Super I/O chips
  4. * Currently, only W83627UHG (Nuvoton NCT6627UD) is supported.
  5. *
  6. * Author: Maciej S. Szmigiero <mail@maciej.szmigiero.name>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/gpio/driver.h>
  10. #include <linux/ioport.h>
  11. #include <linux/isa.h>
  12. #include <linux/module.h>
  13. #define WB_GPIO_DRIVER_NAME KBUILD_MODNAME
  14. #define WB_SIO_BASE 0x2e
  15. #define WB_SIO_BASE_HIGH 0x4e
  16. #define WB_SIO_EXT_ENTER_KEY 0x87
  17. #define WB_SIO_EXT_EXIT_KEY 0xaa
  18. /* global chip registers */
  19. #define WB_SIO_REG_LOGICAL 0x07
  20. #define WB_SIO_REG_CHIP_MSB 0x20
  21. #define WB_SIO_REG_CHIP_LSB 0x21
  22. #define WB_SIO_CHIP_ID_W83627UHG 0xa230
  23. #define WB_SIO_CHIP_ID_W83627UHG_MASK GENMASK(15, 4)
  24. #define WB_SIO_REG_DPD 0x22
  25. #define WB_SIO_REG_DPD_UARTA 4
  26. #define WB_SIO_REG_DPD_UARTB 5
  27. #define WB_SIO_REG_IDPD 0x23
  28. #define WB_SIO_REG_IDPD_UARTC 4
  29. #define WB_SIO_REG_IDPD_UARTD 5
  30. #define WB_SIO_REG_IDPD_UARTE 6
  31. #define WB_SIO_REG_IDPD_UARTF 7
  32. #define WB_SIO_REG_GLOBAL_OPT 0x24
  33. #define WB_SIO_REG_GO_ENFDC 1
  34. #define WB_SIO_REG_OVTGPIO3456 0x29
  35. #define WB_SIO_REG_OG3456_G3PP 3
  36. #define WB_SIO_REG_OG3456_G4PP 4
  37. #define WB_SIO_REG_OG3456_G5PP 5
  38. #define WB_SIO_REG_OG3456_G6PP 7
  39. #define WB_SIO_REG_I2C_PS 0x2a
  40. #define WB_SIO_REG_I2CPS_I2CFS 1
  41. #define WB_SIO_REG_GPIO1_MF 0x2c
  42. #define WB_SIO_REG_G1MF_G1PP 6
  43. #define WB_SIO_REG_G1MF_G2PP 7
  44. #define WB_SIO_REG_G1MF_FS_MASK GENMASK(1, 0)
  45. #define WB_SIO_REG_G1MF_FS_IR_OFF 0
  46. #define WB_SIO_REG_G1MF_FS_IR 1
  47. #define WB_SIO_REG_G1MF_FS_GPIO1 2
  48. #define WB_SIO_REG_G1MF_FS_UARTB 3
  49. /* not an actual device number, just a value meaning 'no device' */
  50. #define WB_SIO_DEV_NONE 0xff
  51. /* registers with offsets >= 0x30 are specific for a particular device */
  52. /* UART B logical device */
  53. #define WB_SIO_DEV_UARTB 0x03
  54. #define WB_SIO_UARTB_REG_ENABLE 0x30
  55. #define WB_SIO_UARTB_ENABLE_ON 0
  56. /* UART C logical device */
  57. #define WB_SIO_DEV_UARTC 0x06
  58. #define WB_SIO_UARTC_REG_ENABLE 0x30
  59. #define WB_SIO_UARTC_ENABLE_ON 0
  60. /* GPIO3, GPIO4 logical device */
  61. #define WB_SIO_DEV_GPIO34 0x07
  62. #define WB_SIO_GPIO34_REG_ENABLE 0x30
  63. #define WB_SIO_GPIO34_ENABLE_3 0
  64. #define WB_SIO_GPIO34_ENABLE_4 1
  65. #define WB_SIO_GPIO34_REG_IO3 0xe0
  66. #define WB_SIO_GPIO34_REG_DATA3 0xe1
  67. #define WB_SIO_GPIO34_REG_INV3 0xe2
  68. #define WB_SIO_GPIO34_REG_IO4 0xe4
  69. #define WB_SIO_GPIO34_REG_DATA4 0xe5
  70. #define WB_SIO_GPIO34_REG_INV4 0xe6
  71. /* WDTO, PLED, GPIO5, GPIO6 logical device */
  72. #define WB_SIO_DEV_WDGPIO56 0x08
  73. #define WB_SIO_WDGPIO56_REG_ENABLE 0x30
  74. #define WB_SIO_WDGPIO56_ENABLE_5 1
  75. #define WB_SIO_WDGPIO56_ENABLE_6 2
  76. #define WB_SIO_WDGPIO56_REG_IO5 0xe0
  77. #define WB_SIO_WDGPIO56_REG_DATA5 0xe1
  78. #define WB_SIO_WDGPIO56_REG_INV5 0xe2
  79. #define WB_SIO_WDGPIO56_REG_IO6 0xe4
  80. #define WB_SIO_WDGPIO56_REG_DATA6 0xe5
  81. #define WB_SIO_WDGPIO56_REG_INV6 0xe6
  82. /* GPIO1, GPIO2, SUSLED logical device */
  83. #define WB_SIO_DEV_GPIO12 0x09
  84. #define WB_SIO_GPIO12_REG_ENABLE 0x30
  85. #define WB_SIO_GPIO12_ENABLE_1 0
  86. #define WB_SIO_GPIO12_ENABLE_2 1
  87. #define WB_SIO_GPIO12_REG_IO1 0xe0
  88. #define WB_SIO_GPIO12_REG_DATA1 0xe1
  89. #define WB_SIO_GPIO12_REG_INV1 0xe2
  90. #define WB_SIO_GPIO12_REG_IO2 0xe4
  91. #define WB_SIO_GPIO12_REG_DATA2 0xe5
  92. #define WB_SIO_GPIO12_REG_INV2 0xe6
  93. /* UART D logical device */
  94. #define WB_SIO_DEV_UARTD 0x0d
  95. #define WB_SIO_UARTD_REG_ENABLE 0x30
  96. #define WB_SIO_UARTD_ENABLE_ON 0
  97. /* UART E logical device */
  98. #define WB_SIO_DEV_UARTE 0x0e
  99. #define WB_SIO_UARTE_REG_ENABLE 0x30
  100. #define WB_SIO_UARTE_ENABLE_ON 0
  101. /*
  102. * for a description what a particular field of this struct means please see
  103. * a description of the relevant module parameter at the bottom of this file
  104. */
  105. struct winbond_gpio_params {
  106. unsigned long base;
  107. unsigned long gpios;
  108. unsigned long ppgpios;
  109. unsigned long odgpios;
  110. bool pledgpio;
  111. bool beepgpio;
  112. bool i2cgpio;
  113. };
  114. static struct winbond_gpio_params params;
  115. static int winbond_sio_enter(unsigned long base)
  116. {
  117. if (!request_muxed_region(base, 2, WB_GPIO_DRIVER_NAME))
  118. return -EBUSY;
  119. /*
  120. * datasheet says two successive writes of the "key" value are needed
  121. * in order for chip to enter the "Extended Function Mode"
  122. */
  123. outb(WB_SIO_EXT_ENTER_KEY, base);
  124. outb(WB_SIO_EXT_ENTER_KEY, base);
  125. return 0;
  126. }
  127. static void winbond_sio_select_logical(unsigned long base, u8 dev)
  128. {
  129. outb(WB_SIO_REG_LOGICAL, base);
  130. outb(dev, base + 1);
  131. }
  132. static void winbond_sio_leave(unsigned long base)
  133. {
  134. outb(WB_SIO_EXT_EXIT_KEY, base);
  135. release_region(base, 2);
  136. }
  137. static void winbond_sio_reg_write(unsigned long base, u8 reg, u8 data)
  138. {
  139. outb(reg, base);
  140. outb(data, base + 1);
  141. }
  142. static u8 winbond_sio_reg_read(unsigned long base, u8 reg)
  143. {
  144. outb(reg, base);
  145. return inb(base + 1);
  146. }
  147. static void winbond_sio_reg_bset(unsigned long base, u8 reg, u8 bit)
  148. {
  149. u8 val;
  150. val = winbond_sio_reg_read(base, reg);
  151. val |= BIT(bit);
  152. winbond_sio_reg_write(base, reg, val);
  153. }
  154. static void winbond_sio_reg_bclear(unsigned long base, u8 reg, u8 bit)
  155. {
  156. u8 val;
  157. val = winbond_sio_reg_read(base, reg);
  158. val &= ~BIT(bit);
  159. winbond_sio_reg_write(base, reg, val);
  160. }
  161. static bool winbond_sio_reg_btest(unsigned long base, u8 reg, u8 bit)
  162. {
  163. return winbond_sio_reg_read(base, reg) & BIT(bit);
  164. }
  165. /**
  166. * struct winbond_gpio_port_conflict - possibly conflicting device information
  167. * @name: device name (NULL means no conflicting device defined)
  168. * @dev: Super I/O logical device number where the testreg register
  169. * is located (or WB_SIO_DEV_NONE - don't select any
  170. * logical device)
  171. * @testreg: register number where the testbit bit is located
  172. * @testbit: index of a bit to check whether an actual conflict exists
  173. * @warnonly: if set then a conflict isn't fatal (just warn about it),
  174. * otherwise disable the particular GPIO port if a conflict
  175. * is detected
  176. */
  177. struct winbond_gpio_port_conflict {
  178. const char *name;
  179. u8 dev;
  180. u8 testreg;
  181. u8 testbit;
  182. bool warnonly;
  183. };
  184. /**
  185. * struct winbond_gpio_info - information about a particular GPIO port (device)
  186. * @dev: Super I/O logical device number of the registers
  187. * specified below
  188. * @enablereg: port enable bit register number
  189. * @enablebit: index of a port enable bit
  190. * @outputreg: output driver mode bit register number
  191. * @outputppbit: index of a push-pull output driver mode bit
  192. * @ioreg: data direction register number
  193. * @invreg: pin data inversion register number
  194. * @datareg: pin data register number
  195. * @conflict: description of a device that possibly conflicts with
  196. * this port
  197. */
  198. struct winbond_gpio_info {
  199. u8 dev;
  200. u8 enablereg;
  201. u8 enablebit;
  202. u8 outputreg;
  203. u8 outputppbit;
  204. u8 ioreg;
  205. u8 invreg;
  206. u8 datareg;
  207. struct winbond_gpio_port_conflict conflict;
  208. };
  209. static const struct winbond_gpio_info winbond_gpio_infos[6] = {
  210. { /* 0 */
  211. .dev = WB_SIO_DEV_GPIO12,
  212. .enablereg = WB_SIO_GPIO12_REG_ENABLE,
  213. .enablebit = WB_SIO_GPIO12_ENABLE_1,
  214. .outputreg = WB_SIO_REG_GPIO1_MF,
  215. .outputppbit = WB_SIO_REG_G1MF_G1PP,
  216. .ioreg = WB_SIO_GPIO12_REG_IO1,
  217. .invreg = WB_SIO_GPIO12_REG_INV1,
  218. .datareg = WB_SIO_GPIO12_REG_DATA1,
  219. .conflict = {
  220. .name = "UARTB",
  221. .dev = WB_SIO_DEV_UARTB,
  222. .testreg = WB_SIO_UARTB_REG_ENABLE,
  223. .testbit = WB_SIO_UARTB_ENABLE_ON,
  224. .warnonly = true
  225. }
  226. },
  227. { /* 1 */
  228. .dev = WB_SIO_DEV_GPIO12,
  229. .enablereg = WB_SIO_GPIO12_REG_ENABLE,
  230. .enablebit = WB_SIO_GPIO12_ENABLE_2,
  231. .outputreg = WB_SIO_REG_GPIO1_MF,
  232. .outputppbit = WB_SIO_REG_G1MF_G2PP,
  233. .ioreg = WB_SIO_GPIO12_REG_IO2,
  234. .invreg = WB_SIO_GPIO12_REG_INV2,
  235. .datareg = WB_SIO_GPIO12_REG_DATA2
  236. /* special conflict handling so doesn't use conflict data */
  237. },
  238. { /* 2 */
  239. .dev = WB_SIO_DEV_GPIO34,
  240. .enablereg = WB_SIO_GPIO34_REG_ENABLE,
  241. .enablebit = WB_SIO_GPIO34_ENABLE_3,
  242. .outputreg = WB_SIO_REG_OVTGPIO3456,
  243. .outputppbit = WB_SIO_REG_OG3456_G3PP,
  244. .ioreg = WB_SIO_GPIO34_REG_IO3,
  245. .invreg = WB_SIO_GPIO34_REG_INV3,
  246. .datareg = WB_SIO_GPIO34_REG_DATA3,
  247. .conflict = {
  248. .name = "UARTC",
  249. .dev = WB_SIO_DEV_UARTC,
  250. .testreg = WB_SIO_UARTC_REG_ENABLE,
  251. .testbit = WB_SIO_UARTC_ENABLE_ON,
  252. .warnonly = true
  253. }
  254. },
  255. { /* 3 */
  256. .dev = WB_SIO_DEV_GPIO34,
  257. .enablereg = WB_SIO_GPIO34_REG_ENABLE,
  258. .enablebit = WB_SIO_GPIO34_ENABLE_4,
  259. .outputreg = WB_SIO_REG_OVTGPIO3456,
  260. .outputppbit = WB_SIO_REG_OG3456_G4PP,
  261. .ioreg = WB_SIO_GPIO34_REG_IO4,
  262. .invreg = WB_SIO_GPIO34_REG_INV4,
  263. .datareg = WB_SIO_GPIO34_REG_DATA4,
  264. .conflict = {
  265. .name = "UARTD",
  266. .dev = WB_SIO_DEV_UARTD,
  267. .testreg = WB_SIO_UARTD_REG_ENABLE,
  268. .testbit = WB_SIO_UARTD_ENABLE_ON,
  269. .warnonly = true
  270. }
  271. },
  272. { /* 4 */
  273. .dev = WB_SIO_DEV_WDGPIO56,
  274. .enablereg = WB_SIO_WDGPIO56_REG_ENABLE,
  275. .enablebit = WB_SIO_WDGPIO56_ENABLE_5,
  276. .outputreg = WB_SIO_REG_OVTGPIO3456,
  277. .outputppbit = WB_SIO_REG_OG3456_G5PP,
  278. .ioreg = WB_SIO_WDGPIO56_REG_IO5,
  279. .invreg = WB_SIO_WDGPIO56_REG_INV5,
  280. .datareg = WB_SIO_WDGPIO56_REG_DATA5,
  281. .conflict = {
  282. .name = "UARTE",
  283. .dev = WB_SIO_DEV_UARTE,
  284. .testreg = WB_SIO_UARTE_REG_ENABLE,
  285. .testbit = WB_SIO_UARTE_ENABLE_ON,
  286. .warnonly = true
  287. }
  288. },
  289. { /* 5 */
  290. .dev = WB_SIO_DEV_WDGPIO56,
  291. .enablereg = WB_SIO_WDGPIO56_REG_ENABLE,
  292. .enablebit = WB_SIO_WDGPIO56_ENABLE_6,
  293. .outputreg = WB_SIO_REG_OVTGPIO3456,
  294. .outputppbit = WB_SIO_REG_OG3456_G6PP,
  295. .ioreg = WB_SIO_WDGPIO56_REG_IO6,
  296. .invreg = WB_SIO_WDGPIO56_REG_INV6,
  297. .datareg = WB_SIO_WDGPIO56_REG_DATA6,
  298. .conflict = {
  299. .name = "FDC",
  300. .dev = WB_SIO_DEV_NONE,
  301. .testreg = WB_SIO_REG_GLOBAL_OPT,
  302. .testbit = WB_SIO_REG_GO_ENFDC,
  303. .warnonly = false
  304. }
  305. }
  306. };
  307. /* returns whether changing a pin is allowed */
  308. static bool winbond_gpio_get_info(unsigned int *gpio_num,
  309. const struct winbond_gpio_info **info)
  310. {
  311. bool allow_changing = true;
  312. unsigned long i;
  313. for_each_set_bit(i, &params.gpios, BITS_PER_LONG) {
  314. if (*gpio_num < 8)
  315. break;
  316. *gpio_num -= 8;
  317. }
  318. *info = &winbond_gpio_infos[i];
  319. /*
  320. * GPIO2 (the second port) shares some pins with a basic PC
  321. * functionality, which is very likely controlled by the firmware.
  322. * Don't allow changing these pins by default.
  323. */
  324. if (i == 1) {
  325. if (*gpio_num == 0 && !params.pledgpio)
  326. allow_changing = false;
  327. else if (*gpio_num == 1 && !params.beepgpio)
  328. allow_changing = false;
  329. else if ((*gpio_num == 5 || *gpio_num == 6) && !params.i2cgpio)
  330. allow_changing = false;
  331. }
  332. return allow_changing;
  333. }
  334. static int winbond_gpio_get(struct gpio_chip *gc, unsigned int offset)
  335. {
  336. unsigned long *base = gpiochip_get_data(gc);
  337. const struct winbond_gpio_info *info;
  338. bool val;
  339. winbond_gpio_get_info(&offset, &info);
  340. val = winbond_sio_enter(*base);
  341. if (val)
  342. return val;
  343. winbond_sio_select_logical(*base, info->dev);
  344. val = winbond_sio_reg_btest(*base, info->datareg, offset);
  345. if (winbond_sio_reg_btest(*base, info->invreg, offset))
  346. val = !val;
  347. winbond_sio_leave(*base);
  348. return val;
  349. }
  350. static int winbond_gpio_direction_in(struct gpio_chip *gc, unsigned int offset)
  351. {
  352. unsigned long *base = gpiochip_get_data(gc);
  353. const struct winbond_gpio_info *info;
  354. int ret;
  355. if (!winbond_gpio_get_info(&offset, &info))
  356. return -EACCES;
  357. ret = winbond_sio_enter(*base);
  358. if (ret)
  359. return ret;
  360. winbond_sio_select_logical(*base, info->dev);
  361. winbond_sio_reg_bset(*base, info->ioreg, offset);
  362. winbond_sio_leave(*base);
  363. return 0;
  364. }
  365. static int winbond_gpio_direction_out(struct gpio_chip *gc,
  366. unsigned int offset,
  367. int val)
  368. {
  369. unsigned long *base = gpiochip_get_data(gc);
  370. const struct winbond_gpio_info *info;
  371. int ret;
  372. if (!winbond_gpio_get_info(&offset, &info))
  373. return -EACCES;
  374. ret = winbond_sio_enter(*base);
  375. if (ret)
  376. return ret;
  377. winbond_sio_select_logical(*base, info->dev);
  378. winbond_sio_reg_bclear(*base, info->ioreg, offset);
  379. if (winbond_sio_reg_btest(*base, info->invreg, offset))
  380. val = !val;
  381. if (val)
  382. winbond_sio_reg_bset(*base, info->datareg, offset);
  383. else
  384. winbond_sio_reg_bclear(*base, info->datareg, offset);
  385. winbond_sio_leave(*base);
  386. return 0;
  387. }
  388. static void winbond_gpio_set(struct gpio_chip *gc, unsigned int offset,
  389. int val)
  390. {
  391. unsigned long *base = gpiochip_get_data(gc);
  392. const struct winbond_gpio_info *info;
  393. if (!winbond_gpio_get_info(&offset, &info))
  394. return;
  395. if (winbond_sio_enter(*base) != 0)
  396. return;
  397. winbond_sio_select_logical(*base, info->dev);
  398. if (winbond_sio_reg_btest(*base, info->invreg, offset))
  399. val = !val;
  400. if (val)
  401. winbond_sio_reg_bset(*base, info->datareg, offset);
  402. else
  403. winbond_sio_reg_bclear(*base, info->datareg, offset);
  404. winbond_sio_leave(*base);
  405. }
  406. static struct gpio_chip winbond_gpio_chip = {
  407. .base = -1,
  408. .label = WB_GPIO_DRIVER_NAME,
  409. .owner = THIS_MODULE,
  410. .can_sleep = true,
  411. .get = winbond_gpio_get,
  412. .direction_input = winbond_gpio_direction_in,
  413. .set = winbond_gpio_set,
  414. .direction_output = winbond_gpio_direction_out,
  415. };
  416. static void winbond_gpio_configure_port0_pins(unsigned long base)
  417. {
  418. unsigned int val;
  419. val = winbond_sio_reg_read(base, WB_SIO_REG_GPIO1_MF);
  420. if ((val & WB_SIO_REG_G1MF_FS_MASK) == WB_SIO_REG_G1MF_FS_GPIO1)
  421. return;
  422. pr_warn("GPIO1 pins were connected to something else (%.2x), fixing\n",
  423. val);
  424. val &= ~WB_SIO_REG_G1MF_FS_MASK;
  425. val |= WB_SIO_REG_G1MF_FS_GPIO1;
  426. winbond_sio_reg_write(base, WB_SIO_REG_GPIO1_MF, val);
  427. }
  428. static void winbond_gpio_configure_port1_check_i2c(unsigned long base)
  429. {
  430. params.i2cgpio = !winbond_sio_reg_btest(base, WB_SIO_REG_I2C_PS,
  431. WB_SIO_REG_I2CPS_I2CFS);
  432. if (!params.i2cgpio)
  433. pr_warn("disabling GPIO2.5 and GPIO2.6 as I2C is enabled\n");
  434. }
  435. static bool winbond_gpio_configure_port(unsigned long base, unsigned int idx)
  436. {
  437. const struct winbond_gpio_info *info = &winbond_gpio_infos[idx];
  438. const struct winbond_gpio_port_conflict *conflict = &info->conflict;
  439. /* is there a possible conflicting device defined? */
  440. if (conflict->name != NULL) {
  441. if (conflict->dev != WB_SIO_DEV_NONE)
  442. winbond_sio_select_logical(base, conflict->dev);
  443. if (winbond_sio_reg_btest(base, conflict->testreg,
  444. conflict->testbit)) {
  445. if (conflict->warnonly)
  446. pr_warn("enabled GPIO%u share pins with active %s\n",
  447. idx + 1, conflict->name);
  448. else {
  449. pr_warn("disabling GPIO%u as %s is enabled\n",
  450. idx + 1, conflict->name);
  451. return false;
  452. }
  453. }
  454. }
  455. /* GPIO1 and GPIO2 need some (additional) special handling */
  456. if (idx == 0)
  457. winbond_gpio_configure_port0_pins(base);
  458. else if (idx == 1)
  459. winbond_gpio_configure_port1_check_i2c(base);
  460. winbond_sio_select_logical(base, info->dev);
  461. winbond_sio_reg_bset(base, info->enablereg, info->enablebit);
  462. if (params.ppgpios & BIT(idx))
  463. winbond_sio_reg_bset(base, info->outputreg,
  464. info->outputppbit);
  465. else if (params.odgpios & BIT(idx))
  466. winbond_sio_reg_bclear(base, info->outputreg,
  467. info->outputppbit);
  468. else
  469. pr_notice("GPIO%u pins are %s\n", idx + 1,
  470. winbond_sio_reg_btest(base, info->outputreg,
  471. info->outputppbit) ?
  472. "push-pull" :
  473. "open drain");
  474. return true;
  475. }
  476. static int winbond_gpio_configure(unsigned long base)
  477. {
  478. unsigned long i;
  479. for_each_set_bit(i, &params.gpios, BITS_PER_LONG)
  480. if (!winbond_gpio_configure_port(base, i))
  481. __clear_bit(i, &params.gpios);
  482. if (!params.gpios) {
  483. pr_err("please use 'gpios' module parameter to select some active GPIO ports to enable\n");
  484. return -EINVAL;
  485. }
  486. return 0;
  487. }
  488. static int winbond_gpio_check_chip(unsigned long base)
  489. {
  490. int ret;
  491. unsigned int chip;
  492. ret = winbond_sio_enter(base);
  493. if (ret)
  494. return ret;
  495. chip = winbond_sio_reg_read(base, WB_SIO_REG_CHIP_MSB) << 8;
  496. chip |= winbond_sio_reg_read(base, WB_SIO_REG_CHIP_LSB);
  497. pr_notice("chip ID at %lx is %.4x\n", base, chip);
  498. if ((chip & WB_SIO_CHIP_ID_W83627UHG_MASK) !=
  499. WB_SIO_CHIP_ID_W83627UHG) {
  500. pr_err("not an our chip\n");
  501. ret = -ENODEV;
  502. }
  503. winbond_sio_leave(base);
  504. return ret;
  505. }
  506. static int winbond_gpio_imatch(struct device *dev, unsigned int id)
  507. {
  508. unsigned long gpios_rem;
  509. int ret;
  510. gpios_rem = params.gpios & ~GENMASK(ARRAY_SIZE(winbond_gpio_infos) - 1,
  511. 0);
  512. if (gpios_rem) {
  513. pr_warn("unknown ports (%lx) enabled in GPIO ports bitmask\n",
  514. gpios_rem);
  515. params.gpios &= ~gpios_rem;
  516. }
  517. if (params.ppgpios & params.odgpios) {
  518. pr_err("some GPIO ports are set both to push-pull and open drain mode at the same time\n");
  519. return 0;
  520. }
  521. if (params.base != 0)
  522. return winbond_gpio_check_chip(params.base) == 0;
  523. /*
  524. * if the 'base' module parameter is unset probe two chip default
  525. * I/O port bases
  526. */
  527. params.base = WB_SIO_BASE;
  528. ret = winbond_gpio_check_chip(params.base);
  529. if (ret == 0)
  530. return 1;
  531. if (ret != -ENODEV && ret != -EBUSY)
  532. return 0;
  533. params.base = WB_SIO_BASE_HIGH;
  534. return winbond_gpio_check_chip(params.base) == 0;
  535. }
  536. static int winbond_gpio_iprobe(struct device *dev, unsigned int id)
  537. {
  538. int ret;
  539. if (params.base == 0)
  540. return -EINVAL;
  541. ret = winbond_sio_enter(params.base);
  542. if (ret)
  543. return ret;
  544. ret = winbond_gpio_configure(params.base);
  545. winbond_sio_leave(params.base);
  546. if (ret)
  547. return ret;
  548. /*
  549. * Add 8 gpios for every GPIO port that was enabled in gpios
  550. * module parameter (that wasn't disabled earlier in
  551. * winbond_gpio_configure() & co. due to, for example, a pin conflict).
  552. */
  553. winbond_gpio_chip.ngpio = hweight_long(params.gpios) * 8;
  554. /*
  555. * GPIO6 port has only 5 pins, so if it is enabled we have to adjust
  556. * the total count appropriately
  557. */
  558. if (params.gpios & BIT(5))
  559. winbond_gpio_chip.ngpio -= (8 - 5);
  560. winbond_gpio_chip.parent = dev;
  561. return devm_gpiochip_add_data(dev, &winbond_gpio_chip, &params.base);
  562. }
  563. static struct isa_driver winbond_gpio_idriver = {
  564. .driver = {
  565. .name = WB_GPIO_DRIVER_NAME,
  566. },
  567. .match = winbond_gpio_imatch,
  568. .probe = winbond_gpio_iprobe,
  569. };
  570. module_isa_driver(winbond_gpio_idriver, 1);
  571. module_param_named(base, params.base, ulong, 0444);
  572. MODULE_PARM_DESC(base,
  573. "I/O port base (when unset - probe chip default ones)");
  574. /* This parameter sets which GPIO devices (ports) we enable */
  575. module_param_named(gpios, params.gpios, ulong, 0444);
  576. MODULE_PARM_DESC(gpios,
  577. "bitmask of GPIO ports to enable (bit 0 - GPIO1, bit 1 - GPIO2, etc.");
  578. /*
  579. * These two parameters below set how we configure GPIO ports output drivers.
  580. * It can't be a one bitmask since we need three values per port: push-pull,
  581. * open-drain and keep as-is (this is the default).
  582. */
  583. module_param_named(ppgpios, params.ppgpios, ulong, 0444);
  584. MODULE_PARM_DESC(ppgpios,
  585. "bitmask of GPIO ports to set to push-pull mode (bit 0 - GPIO1, bit 1 - GPIO2, etc.");
  586. module_param_named(odgpios, params.odgpios, ulong, 0444);
  587. MODULE_PARM_DESC(odgpios,
  588. "bitmask of GPIO ports to set to open drain mode (bit 0 - GPIO1, bit 1 - GPIO2, etc.");
  589. /*
  590. * GPIO2.0 and GPIO2.1 control a basic PC functionality that we
  591. * don't allow tinkering with by default (it is very likely that the
  592. * firmware owns these pins).
  593. * These two parameters below allow overriding these prohibitions.
  594. */
  595. module_param_named(pledgpio, params.pledgpio, bool, 0644);
  596. MODULE_PARM_DESC(pledgpio,
  597. "enable changing value of GPIO2.0 bit (Power LED), default no.");
  598. module_param_named(beepgpio, params.beepgpio, bool, 0644);
  599. MODULE_PARM_DESC(beepgpio,
  600. "enable changing value of GPIO2.1 bit (BEEP), default no.");
  601. MODULE_AUTHOR("Maciej S. Szmigiero <mail@maciej.szmigiero.name>");
  602. MODULE_DESCRIPTION("GPIO interface for Winbond Super I/O chips");
  603. MODULE_LICENSE("GPL");