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 <[email protected]>
  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. int ret;
  340. winbond_gpio_get_info(&offset, &info);
  341. ret = winbond_sio_enter(*base);
  342. if (ret)
  343. return ret;
  344. winbond_sio_select_logical(*base, info->dev);
  345. val = winbond_sio_reg_btest(*base, info->datareg, offset);
  346. if (winbond_sio_reg_btest(*base, info->invreg, offset))
  347. val = !val;
  348. winbond_sio_leave(*base);
  349. return val;
  350. }
  351. static int winbond_gpio_direction_in(struct gpio_chip *gc, unsigned int offset)
  352. {
  353. unsigned long *base = gpiochip_get_data(gc);
  354. const struct winbond_gpio_info *info;
  355. int ret;
  356. if (!winbond_gpio_get_info(&offset, &info))
  357. return -EACCES;
  358. ret = winbond_sio_enter(*base);
  359. if (ret)
  360. return ret;
  361. winbond_sio_select_logical(*base, info->dev);
  362. winbond_sio_reg_bset(*base, info->ioreg, offset);
  363. winbond_sio_leave(*base);
  364. return 0;
  365. }
  366. static int winbond_gpio_direction_out(struct gpio_chip *gc,
  367. unsigned int offset,
  368. int val)
  369. {
  370. unsigned long *base = gpiochip_get_data(gc);
  371. const struct winbond_gpio_info *info;
  372. int ret;
  373. if (!winbond_gpio_get_info(&offset, &info))
  374. return -EACCES;
  375. ret = winbond_sio_enter(*base);
  376. if (ret)
  377. return ret;
  378. winbond_sio_select_logical(*base, info->dev);
  379. winbond_sio_reg_bclear(*base, info->ioreg, offset);
  380. if (winbond_sio_reg_btest(*base, info->invreg, offset))
  381. val = !val;
  382. if (val)
  383. winbond_sio_reg_bset(*base, info->datareg, offset);
  384. else
  385. winbond_sio_reg_bclear(*base, info->datareg, offset);
  386. winbond_sio_leave(*base);
  387. return 0;
  388. }
  389. static void winbond_gpio_set(struct gpio_chip *gc, unsigned int offset,
  390. int val)
  391. {
  392. unsigned long *base = gpiochip_get_data(gc);
  393. const struct winbond_gpio_info *info;
  394. if (!winbond_gpio_get_info(&offset, &info))
  395. return;
  396. if (winbond_sio_enter(*base) != 0)
  397. return;
  398. winbond_sio_select_logical(*base, info->dev);
  399. if (winbond_sio_reg_btest(*base, info->invreg, offset))
  400. val = !val;
  401. if (val)
  402. winbond_sio_reg_bset(*base, info->datareg, offset);
  403. else
  404. winbond_sio_reg_bclear(*base, info->datareg, offset);
  405. winbond_sio_leave(*base);
  406. }
  407. static struct gpio_chip winbond_gpio_chip = {
  408. .base = -1,
  409. .label = WB_GPIO_DRIVER_NAME,
  410. .owner = THIS_MODULE,
  411. .can_sleep = true,
  412. .get = winbond_gpio_get,
  413. .direction_input = winbond_gpio_direction_in,
  414. .set = winbond_gpio_set,
  415. .direction_output = winbond_gpio_direction_out,
  416. };
  417. static void winbond_gpio_configure_port0_pins(unsigned long base)
  418. {
  419. unsigned int val;
  420. val = winbond_sio_reg_read(base, WB_SIO_REG_GPIO1_MF);
  421. if ((val & WB_SIO_REG_G1MF_FS_MASK) == WB_SIO_REG_G1MF_FS_GPIO1)
  422. return;
  423. pr_warn("GPIO1 pins were connected to something else (%.2x), fixing\n",
  424. val);
  425. val &= ~WB_SIO_REG_G1MF_FS_MASK;
  426. val |= WB_SIO_REG_G1MF_FS_GPIO1;
  427. winbond_sio_reg_write(base, WB_SIO_REG_GPIO1_MF, val);
  428. }
  429. static void winbond_gpio_configure_port1_check_i2c(unsigned long base)
  430. {
  431. params.i2cgpio = !winbond_sio_reg_btest(base, WB_SIO_REG_I2C_PS,
  432. WB_SIO_REG_I2CPS_I2CFS);
  433. if (!params.i2cgpio)
  434. pr_warn("disabling GPIO2.5 and GPIO2.6 as I2C is enabled\n");
  435. }
  436. static bool winbond_gpio_configure_port(unsigned long base, unsigned int idx)
  437. {
  438. const struct winbond_gpio_info *info = &winbond_gpio_infos[idx];
  439. const struct winbond_gpio_port_conflict *conflict = &info->conflict;
  440. /* is there a possible conflicting device defined? */
  441. if (conflict->name != NULL) {
  442. if (conflict->dev != WB_SIO_DEV_NONE)
  443. winbond_sio_select_logical(base, conflict->dev);
  444. if (winbond_sio_reg_btest(base, conflict->testreg,
  445. conflict->testbit)) {
  446. if (conflict->warnonly)
  447. pr_warn("enabled GPIO%u share pins with active %s\n",
  448. idx + 1, conflict->name);
  449. else {
  450. pr_warn("disabling GPIO%u as %s is enabled\n",
  451. idx + 1, conflict->name);
  452. return false;
  453. }
  454. }
  455. }
  456. /* GPIO1 and GPIO2 need some (additional) special handling */
  457. if (idx == 0)
  458. winbond_gpio_configure_port0_pins(base);
  459. else if (idx == 1)
  460. winbond_gpio_configure_port1_check_i2c(base);
  461. winbond_sio_select_logical(base, info->dev);
  462. winbond_sio_reg_bset(base, info->enablereg, info->enablebit);
  463. if (params.ppgpios & BIT(idx))
  464. winbond_sio_reg_bset(base, info->outputreg,
  465. info->outputppbit);
  466. else if (params.odgpios & BIT(idx))
  467. winbond_sio_reg_bclear(base, info->outputreg,
  468. info->outputppbit);
  469. else
  470. pr_notice("GPIO%u pins are %s\n", idx + 1,
  471. winbond_sio_reg_btest(base, info->outputreg,
  472. info->outputppbit) ?
  473. "push-pull" :
  474. "open drain");
  475. return true;
  476. }
  477. static int winbond_gpio_configure(unsigned long base)
  478. {
  479. unsigned long i;
  480. for_each_set_bit(i, &params.gpios, BITS_PER_LONG)
  481. if (!winbond_gpio_configure_port(base, i))
  482. __clear_bit(i, &params.gpios);
  483. if (!params.gpios) {
  484. pr_err("please use 'gpios' module parameter to select some active GPIO ports to enable\n");
  485. return -EINVAL;
  486. }
  487. return 0;
  488. }
  489. static int winbond_gpio_check_chip(unsigned long base)
  490. {
  491. int ret;
  492. unsigned int chip;
  493. ret = winbond_sio_enter(base);
  494. if (ret)
  495. return ret;
  496. chip = winbond_sio_reg_read(base, WB_SIO_REG_CHIP_MSB) << 8;
  497. chip |= winbond_sio_reg_read(base, WB_SIO_REG_CHIP_LSB);
  498. pr_notice("chip ID at %lx is %.4x\n", base, chip);
  499. if ((chip & WB_SIO_CHIP_ID_W83627UHG_MASK) !=
  500. WB_SIO_CHIP_ID_W83627UHG) {
  501. pr_err("not an our chip\n");
  502. ret = -ENODEV;
  503. }
  504. winbond_sio_leave(base);
  505. return ret;
  506. }
  507. static int winbond_gpio_imatch(struct device *dev, unsigned int id)
  508. {
  509. unsigned long gpios_rem;
  510. int ret;
  511. gpios_rem = params.gpios & ~GENMASK(ARRAY_SIZE(winbond_gpio_infos) - 1,
  512. 0);
  513. if (gpios_rem) {
  514. pr_warn("unknown ports (%lx) enabled in GPIO ports bitmask\n",
  515. gpios_rem);
  516. params.gpios &= ~gpios_rem;
  517. }
  518. if (params.ppgpios & params.odgpios) {
  519. pr_err("some GPIO ports are set both to push-pull and open drain mode at the same time\n");
  520. return 0;
  521. }
  522. if (params.base != 0)
  523. return winbond_gpio_check_chip(params.base) == 0;
  524. /*
  525. * if the 'base' module parameter is unset probe two chip default
  526. * I/O port bases
  527. */
  528. params.base = WB_SIO_BASE;
  529. ret = winbond_gpio_check_chip(params.base);
  530. if (ret == 0)
  531. return 1;
  532. if (ret != -ENODEV && ret != -EBUSY)
  533. return 0;
  534. params.base = WB_SIO_BASE_HIGH;
  535. return winbond_gpio_check_chip(params.base) == 0;
  536. }
  537. static int winbond_gpio_iprobe(struct device *dev, unsigned int id)
  538. {
  539. int ret;
  540. if (params.base == 0)
  541. return -EINVAL;
  542. ret = winbond_sio_enter(params.base);
  543. if (ret)
  544. return ret;
  545. ret = winbond_gpio_configure(params.base);
  546. winbond_sio_leave(params.base);
  547. if (ret)
  548. return ret;
  549. /*
  550. * Add 8 gpios for every GPIO port that was enabled in gpios
  551. * module parameter (that wasn't disabled earlier in
  552. * winbond_gpio_configure() & co. due to, for example, a pin conflict).
  553. */
  554. winbond_gpio_chip.ngpio = hweight_long(params.gpios) * 8;
  555. /*
  556. * GPIO6 port has only 5 pins, so if it is enabled we have to adjust
  557. * the total count appropriately
  558. */
  559. if (params.gpios & BIT(5))
  560. winbond_gpio_chip.ngpio -= (8 - 5);
  561. winbond_gpio_chip.parent = dev;
  562. return devm_gpiochip_add_data(dev, &winbond_gpio_chip, &params.base);
  563. }
  564. static struct isa_driver winbond_gpio_idriver = {
  565. .driver = {
  566. .name = WB_GPIO_DRIVER_NAME,
  567. },
  568. .match = winbond_gpio_imatch,
  569. .probe = winbond_gpio_iprobe,
  570. };
  571. module_isa_driver(winbond_gpio_idriver, 1);
  572. module_param_named(base, params.base, ulong, 0444);
  573. MODULE_PARM_DESC(base,
  574. "I/O port base (when unset - probe chip default ones)");
  575. /* This parameter sets which GPIO devices (ports) we enable */
  576. module_param_named(gpios, params.gpios, ulong, 0444);
  577. MODULE_PARM_DESC(gpios,
  578. "bitmask of GPIO ports to enable (bit 0 - GPIO1, bit 1 - GPIO2, etc.");
  579. /*
  580. * These two parameters below set how we configure GPIO ports output drivers.
  581. * It can't be a one bitmask since we need three values per port: push-pull,
  582. * open-drain and keep as-is (this is the default).
  583. */
  584. module_param_named(ppgpios, params.ppgpios, ulong, 0444);
  585. MODULE_PARM_DESC(ppgpios,
  586. "bitmask of GPIO ports to set to push-pull mode (bit 0 - GPIO1, bit 1 - GPIO2, etc.");
  587. module_param_named(odgpios, params.odgpios, ulong, 0444);
  588. MODULE_PARM_DESC(odgpios,
  589. "bitmask of GPIO ports to set to open drain mode (bit 0 - GPIO1, bit 1 - GPIO2, etc.");
  590. /*
  591. * GPIO2.0 and GPIO2.1 control a basic PC functionality that we
  592. * don't allow tinkering with by default (it is very likely that the
  593. * firmware owns these pins).
  594. * These two parameters below allow overriding these prohibitions.
  595. */
  596. module_param_named(pledgpio, params.pledgpio, bool, 0644);
  597. MODULE_PARM_DESC(pledgpio,
  598. "enable changing value of GPIO2.0 bit (Power LED), default no.");
  599. module_param_named(beepgpio, params.beepgpio, bool, 0644);
  600. MODULE_PARM_DESC(beepgpio,
  601. "enable changing value of GPIO2.1 bit (BEEP), default no.");
  602. MODULE_AUTHOR("Maciej S. Szmigiero <[email protected]>");
  603. MODULE_DESCRIPTION("GPIO interface for Winbond Super I/O chips");
  604. MODULE_LICENSE("GPL");