483a6f8bd958df57aa3f9729cda7d955905ae0ec
[deliverable/linux.git] / drivers / gpio / gpio-sch311x.c
1 /*
2 * GPIO driver for the SMSC SCH311x Super-I/O chips
3 *
4 * Copyright (C) 2013 Bruno Randolf <br1@einfach.org>
5 *
6 * SuperIO functions and chip detection:
7 * (c) Copyright 2008 Wim Van Sebroeck <wim@iguana.be>.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 */
14
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/platform_device.h>
19 #include <linux/gpio.h>
20 #include <linux/bitops.h>
21
22 #define DRV_NAME "gpio-sch311x"
23
24 #define SCH311X_GPIO_CONF_OUT 0x00
25 #define SCH311X_GPIO_CONF_IN 0x01
26 #define SCH311X_GPIO_CONF_INVERT 0x02
27 #define SCH311X_GPIO_CONF_OPEN_DRAIN 0x80
28
29 #define SIO_CONFIG_KEY_ENTER 0x55
30 #define SIO_CONFIG_KEY_EXIT 0xaa
31
32 #define GP1 0x4b
33
34 static int sch311x_ioports[] = { 0x2e, 0x4e, 0x162e, 0x164e };
35
36 static struct platform_device *sch311x_gpio_pdev;
37
38 struct sch311x_pdev_data { /* platform device data */
39 unsigned short runtime_reg; /* runtime register base address */
40 };
41
42 struct sch311x_gpio_block { /* one GPIO block runtime data */
43 struct gpio_chip chip;
44 unsigned short data_reg; /* from definition below */
45 unsigned short *config_regs; /* pointer to definition below */
46 unsigned short runtime_reg; /* runtime register */
47 spinlock_t lock; /* lock for this GPIO block */
48 };
49
50 struct sch311x_gpio_priv { /* driver private data */
51 struct sch311x_gpio_block blocks[6];
52 };
53
54 struct sch311x_gpio_block_def { /* register address definitions */
55 unsigned short data_reg;
56 unsigned short config_regs[8];
57 unsigned short base;
58 };
59
60 /* Note: some GPIOs are not available, these are marked with 0x00 */
61
62 static struct sch311x_gpio_block_def sch311x_gpio_blocks[] = {
63 {
64 .data_reg = 0x4b, /* GP1 */
65 .config_regs = {0x23, 0x24, 0x25, 0x26, 0x27, 0x29, 0x2a, 0x2b},
66 .base = 10,
67 },
68 {
69 .data_reg = 0x4c, /* GP2 */
70 .config_regs = {0x00, 0x2c, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x32},
71 .base = 20,
72 },
73 {
74 .data_reg = 0x4d, /* GP3 */
75 .config_regs = {0x33, 0x34, 0x35, 0x36, 0x37, 0x00, 0x39, 0x3a},
76 .base = 30,
77 },
78 {
79 .data_reg = 0x4e, /* GP4 */
80 .config_regs = {0x3b, 0x00, 0x3d, 0x00, 0x6e, 0x6f, 0x72, 0x73},
81 .base = 40,
82 },
83 {
84 .data_reg = 0x4f, /* GP5 */
85 .config_regs = {0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46},
86 .base = 50,
87 },
88 {
89 .data_reg = 0x50, /* GP6 */
90 .config_regs = {0x47, 0x48, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59},
91 .base = 60,
92 },
93 };
94
95 static inline struct sch311x_gpio_block *
96 to_sch311x_gpio_block(struct gpio_chip *chip)
97 {
98 return container_of(chip, struct sch311x_gpio_block, chip);
99 }
100
101
102 /*
103 * Super-IO functions
104 */
105
106 static inline int sch311x_sio_enter(int sio_config_port)
107 {
108 /* Don't step on other drivers' I/O space by accident. */
109 if (!request_muxed_region(sio_config_port, 2, DRV_NAME)) {
110 pr_err(DRV_NAME "I/O address 0x%04x already in use\n",
111 sio_config_port);
112 return -EBUSY;
113 }
114
115 outb(SIO_CONFIG_KEY_ENTER, sio_config_port);
116 return 0;
117 }
118
119 static inline void sch311x_sio_exit(int sio_config_port)
120 {
121 outb(SIO_CONFIG_KEY_EXIT, sio_config_port);
122 release_region(sio_config_port, 2);
123 }
124
125 static inline int sch311x_sio_inb(int sio_config_port, int reg)
126 {
127 outb(reg, sio_config_port);
128 return inb(sio_config_port + 1);
129 }
130
131 static inline void sch311x_sio_outb(int sio_config_port, int reg, int val)
132 {
133 outb(reg, sio_config_port);
134 outb(val, sio_config_port + 1);
135 }
136
137
138 /*
139 * GPIO functions
140 */
141
142 static int sch311x_gpio_request(struct gpio_chip *chip, unsigned offset)
143 {
144 struct sch311x_gpio_block *block = to_sch311x_gpio_block(chip);
145
146 if (block->config_regs[offset] == 0) /* GPIO is not available */
147 return -ENODEV;
148
149 if (!request_region(block->runtime_reg + block->config_regs[offset],
150 1, DRV_NAME)) {
151 dev_err(chip->dev, "Failed to request region 0x%04x.\n",
152 block->runtime_reg + block->config_regs[offset]);
153 return -EBUSY;
154 }
155 return 0;
156 }
157
158 static void sch311x_gpio_free(struct gpio_chip *chip, unsigned offset)
159 {
160 struct sch311x_gpio_block *block = to_sch311x_gpio_block(chip);
161
162 if (block->config_regs[offset] == 0) /* GPIO is not available */
163 return;
164
165 release_region(block->runtime_reg + block->config_regs[offset], 1);
166 }
167
168 static int sch311x_gpio_get(struct gpio_chip *chip, unsigned offset)
169 {
170 struct sch311x_gpio_block *block = to_sch311x_gpio_block(chip);
171 unsigned char data;
172
173 spin_lock(&block->lock);
174 data = inb(block->runtime_reg + block->data_reg);
175 spin_unlock(&block->lock);
176
177 return !!(data & BIT(offset));
178 }
179
180 static void __sch311x_gpio_set(struct sch311x_gpio_block *block,
181 unsigned offset, int value)
182 {
183 unsigned char data = inb(block->runtime_reg + block->data_reg);
184 if (value)
185 data |= BIT(offset);
186 else
187 data &= ~BIT(offset);
188 outb(data, block->runtime_reg + block->data_reg);
189 }
190
191 static void sch311x_gpio_set(struct gpio_chip *chip, unsigned offset,
192 int value)
193 {
194 struct sch311x_gpio_block *block = to_sch311x_gpio_block(chip);
195
196 spin_lock(&block->lock);
197 __sch311x_gpio_set(block, offset, value);
198 spin_unlock(&block->lock);
199 }
200
201 static int sch311x_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
202 {
203 struct sch311x_gpio_block *block = to_sch311x_gpio_block(chip);
204
205 spin_lock(&block->lock);
206 outb(SCH311X_GPIO_CONF_IN, block->runtime_reg +
207 block->config_regs[offset]);
208 spin_unlock(&block->lock);
209
210 return 0;
211 }
212
213 static int sch311x_gpio_direction_out(struct gpio_chip *chip, unsigned offset,
214 int value)
215 {
216 struct sch311x_gpio_block *block = to_sch311x_gpio_block(chip);
217
218 spin_lock(&block->lock);
219
220 outb(SCH311X_GPIO_CONF_OUT, block->runtime_reg +
221 block->config_regs[offset]);
222
223 __sch311x_gpio_set(block, offset, value);
224
225 spin_unlock(&block->lock);
226 return 0;
227 }
228
229 static int sch311x_gpio_probe(struct platform_device *pdev)
230 {
231 struct sch311x_pdev_data *pdata = pdev->dev.platform_data;
232 struct sch311x_gpio_priv *priv;
233 struct sch311x_gpio_block *block;
234 int err, i;
235
236 /* we can register all GPIO data registers at once */
237 if (!request_region(pdata->runtime_reg + GP1, 6, DRV_NAME)) {
238 dev_err(&pdev->dev, "Failed to request region 0x%04x-0x%04x.\n",
239 pdata->runtime_reg + GP1, pdata->runtime_reg + GP1 + 5);
240 return -EBUSY;
241 }
242
243 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
244 if (!priv)
245 return -ENOMEM;
246
247 platform_set_drvdata(pdev, priv);
248
249 for (i = 0; i < ARRAY_SIZE(priv->blocks); i++) {
250 block = &priv->blocks[i];
251
252 spin_lock_init(&block->lock);
253
254 block->chip.label = DRV_NAME;
255 block->chip.owner = THIS_MODULE;
256 block->chip.request = sch311x_gpio_request;
257 block->chip.free = sch311x_gpio_free;
258 block->chip.direction_input = sch311x_gpio_direction_in;
259 block->chip.direction_output = sch311x_gpio_direction_out;
260 block->chip.get = sch311x_gpio_get;
261 block->chip.set = sch311x_gpio_set;
262 block->chip.ngpio = 8;
263 block->chip.dev = &pdev->dev;
264 block->chip.base = sch311x_gpio_blocks[i].base;
265 block->config_regs = sch311x_gpio_blocks[i].config_regs;
266 block->data_reg = sch311x_gpio_blocks[i].data_reg;
267 block->runtime_reg = pdata->runtime_reg;
268
269 err = gpiochip_add(&block->chip);
270 if (err < 0) {
271 dev_err(&pdev->dev,
272 "Could not register gpiochip, %d\n", err);
273 goto exit_err;
274 }
275 dev_info(&pdev->dev,
276 "SMSC SCH311x GPIO block %d registered.\n", i);
277 }
278
279 return 0;
280
281 exit_err:
282 release_region(pdata->runtime_reg + GP1, 6);
283 /* release already registered chips */
284 for (--i; i >= 0; i--)
285 gpiochip_remove(&priv->blocks[i].chip);
286 return err;
287 }
288
289 static int sch311x_gpio_remove(struct platform_device *pdev)
290 {
291 struct sch311x_pdev_data *pdata = pdev->dev.platform_data;
292 struct sch311x_gpio_priv *priv = platform_get_drvdata(pdev);
293 int err, i;
294
295 release_region(pdata->runtime_reg + GP1, 6);
296
297 for (i = 0; i < ARRAY_SIZE(priv->blocks); i++) {
298 err = gpiochip_remove(&priv->blocks[i].chip);
299 if (err)
300 return err;
301 dev_info(&pdev->dev,
302 "SMSC SCH311x GPIO block %d unregistered.\n", i);
303 }
304 return 0;
305 }
306
307 static struct platform_driver sch311x_gpio_driver = {
308 .driver.name = DRV_NAME,
309 .driver.owner = THIS_MODULE,
310 .probe = sch311x_gpio_probe,
311 .remove = sch311x_gpio_remove,
312 };
313
314
315 /*
316 * Init & exit routines
317 */
318
319 static int __init sch311x_detect(int sio_config_port, unsigned short *addr)
320 {
321 int err = 0, reg;
322 unsigned short base_addr;
323 unsigned char dev_id;
324
325 err = sch311x_sio_enter(sio_config_port);
326 if (err)
327 return err;
328
329 /* Check device ID. We currently know about:
330 * SCH3112 (0x7c), SCH3114 (0x7d), and SCH3116 (0x7f). */
331 reg = sch311x_sio_inb(sio_config_port, 0x20);
332 if (!(reg == 0x7c || reg == 0x7d || reg == 0x7f)) {
333 err = -ENODEV;
334 goto exit;
335 }
336 dev_id = reg == 0x7c ? 2 : reg == 0x7d ? 4 : 6;
337
338 /* Select logical device A (runtime registers) */
339 sch311x_sio_outb(sio_config_port, 0x07, 0x0a);
340
341 /* Check if Logical Device Register is currently active */
342 if ((sch311x_sio_inb(sio_config_port, 0x30) & 0x01) == 0)
343 pr_info("Seems that LDN 0x0a is not active...\n");
344
345 /* Get the base address of the runtime registers */
346 base_addr = (sch311x_sio_inb(sio_config_port, 0x60) << 8) |
347 sch311x_sio_inb(sio_config_port, 0x61);
348 if (!base_addr) {
349 pr_err("Base address not set\n");
350 err = -ENODEV;
351 goto exit;
352 }
353 *addr = base_addr;
354
355 pr_info("Found an SMSC SCH311%d chip at 0x%04x\n", dev_id, base_addr);
356
357 exit:
358 sch311x_sio_exit(sio_config_port);
359 return err;
360 }
361
362 static int __init sch311x_gpio_pdev_add(const unsigned short addr)
363 {
364 struct sch311x_pdev_data pdata;
365 int err;
366
367 pdata.runtime_reg = addr;
368
369 sch311x_gpio_pdev = platform_device_alloc(DRV_NAME, -1);
370 if (!sch311x_gpio_pdev)
371 return -ENOMEM;
372
373 err = platform_device_add_data(sch311x_gpio_pdev,
374 &pdata, sizeof(pdata));
375 if (err) {
376 pr_err(DRV_NAME "Platform data allocation failed\n");
377 goto err;
378 }
379
380 err = platform_device_add(sch311x_gpio_pdev);
381 if (err) {
382 pr_err(DRV_NAME "Device addition failed\n");
383 goto err;
384 }
385 return 0;
386
387 err:
388 platform_device_put(sch311x_gpio_pdev);
389 return err;
390 }
391
392 static int __init sch311x_gpio_init(void)
393 {
394 int err, i;
395 unsigned short addr = 0;
396
397 for (i = 0; i < ARRAY_SIZE(sch311x_ioports); i++)
398 if (sch311x_detect(sch311x_ioports[i], &addr) == 0)
399 break;
400
401 if (!addr)
402 return -ENODEV;
403
404 err = platform_driver_register(&sch311x_gpio_driver);
405 if (err)
406 return err;
407
408 err = sch311x_gpio_pdev_add(addr);
409 if (err)
410 goto unreg_platform_driver;
411
412 return 0;
413
414 unreg_platform_driver:
415 platform_driver_unregister(&sch311x_gpio_driver);
416 return err;
417 }
418
419 static void __exit sch311x_gpio_exit(void)
420 {
421 platform_device_unregister(sch311x_gpio_pdev);
422 platform_driver_unregister(&sch311x_gpio_driver);
423 }
424
425 module_init(sch311x_gpio_init);
426 module_exit(sch311x_gpio_exit);
427
428 MODULE_AUTHOR("Bruno Randolf <br1@einfach.org>");
429 MODULE_DESCRIPTION("SMSC SCH311x GPIO Driver");
430 MODULE_LICENSE("GPL");
431 MODULE_ALIAS("platform:gpio-sch311x");
This page took 0.040691 seconds and 4 git commands to generate.