blob: 5d518d5db7a0ae4a084f3652cb2ac61067a3dbc6 [file] [log] [blame]
Miguel Gaioead6db082010-10-27 15:33:18 -07001/*
2 * 74Hx164 - Generic serial-in/parallel-out 8-bits shift register GPIO driver
3 *
4 * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2010 Miguel Gaio <miguel.gaio@efixo.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/init.h>
13#include <linux/mutex.h>
14#include <linux/spi/spi.h>
15#include <linux/spi/74x164.h>
16#include <linux/gpio.h>
Maxime Ripard20bc4d52012-09-10 22:35:39 +020017#include <linux/of_gpio.h>
Miguel Gaioead6db082010-10-27 15:33:18 -070018#include <linux/slab.h>
Paul Gortmakerbb207ef2011-07-03 13:38:09 -040019#include <linux/module.h>
Miguel Gaioead6db082010-10-27 15:33:18 -070020
Maxime Ripard20bc4d52012-09-10 22:35:39 +020021#define GEN_74X164_NUMBER_GPIOS 8
22
Miguel Gaioead6db082010-10-27 15:33:18 -070023struct gen_74x164_chip {
24 struct spi_device *spi;
Maxime Ripard20bc4d52012-09-10 22:35:39 +020025 u8 *buffer;
Miguel Gaioead6db082010-10-27 15:33:18 -070026 struct gpio_chip gpio_chip;
27 struct mutex lock;
Maxime Ripard20bc4d52012-09-10 22:35:39 +020028 u32 registers;
Miguel Gaioead6db082010-10-27 15:33:18 -070029};
30
Linus Walleij12610be2011-06-13 10:47:15 +020031static struct gen_74x164_chip *gpio_to_74x164_chip(struct gpio_chip *gc)
Miguel Gaioead6db082010-10-27 15:33:18 -070032{
33 return container_of(gc, struct gen_74x164_chip, gpio_chip);
34}
35
36static int __gen_74x164_write_config(struct gen_74x164_chip *chip)
37{
Maxime Ripard20bc4d52012-09-10 22:35:39 +020038 struct spi_message message;
39 struct spi_transfer *msg_buf;
40 int i, ret = 0;
41
42 msg_buf = kzalloc(chip->registers * sizeof(struct spi_transfer),
43 GFP_KERNEL);
44 if (!msg_buf)
45 return -ENOMEM;
46
47 spi_message_init(&message);
48
49 /*
50 * Since the registers are chained, every byte sent will make
51 * the previous byte shift to the next register in the
52 * chain. Thus, the first byte send will end up in the last
53 * register at the end of the transfer. So, to have a logical
54 * numbering, send the bytes in reverse order so that the last
55 * byte of the buffer will end up in the last register.
56 */
57 for (i = chip->registers - 1; i >= 0; i--) {
58 msg_buf[i].tx_buf = chip->buffer +i;
59 msg_buf[i].len = sizeof(u8);
60 spi_message_add_tail(msg_buf + i, &message);
61 }
62
63 ret = spi_sync(chip->spi, &message);
64
65 kfree(msg_buf);
66
67 return ret;
Miguel Gaioead6db082010-10-27 15:33:18 -070068}
69
Miguel Gaioead6db082010-10-27 15:33:18 -070070static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset)
71{
Linus Walleij12610be2011-06-13 10:47:15 +020072 struct gen_74x164_chip *chip = gpio_to_74x164_chip(gc);
Maxime Ripard20bc4d52012-09-10 22:35:39 +020073 u8 bank = offset / 8;
74 u8 pin = offset % 8;
Miguel Gaioead6db082010-10-27 15:33:18 -070075 int ret;
76
77 mutex_lock(&chip->lock);
Maxime Ripard20bc4d52012-09-10 22:35:39 +020078 ret = (chip->buffer[bank] >> pin) & 0x1;
Miguel Gaioead6db082010-10-27 15:33:18 -070079 mutex_unlock(&chip->lock);
80
81 return ret;
82}
83
84static void gen_74x164_set_value(struct gpio_chip *gc,
85 unsigned offset, int val)
86{
Linus Walleij12610be2011-06-13 10:47:15 +020087 struct gen_74x164_chip *chip = gpio_to_74x164_chip(gc);
Maxime Ripard20bc4d52012-09-10 22:35:39 +020088 u8 bank = offset / 8;
89 u8 pin = offset % 8;
Miguel Gaioead6db082010-10-27 15:33:18 -070090
91 mutex_lock(&chip->lock);
92 if (val)
Maxime Ripard20bc4d52012-09-10 22:35:39 +020093 chip->buffer[bank] |= (1 << pin);
Miguel Gaioead6db082010-10-27 15:33:18 -070094 else
Maxime Ripard20bc4d52012-09-10 22:35:39 +020095 chip->buffer[bank] &= ~(1 << pin);
Miguel Gaioead6db082010-10-27 15:33:18 -070096
97 __gen_74x164_write_config(chip);
98 mutex_unlock(&chip->lock);
99}
100
H Hartley Sweetena3cc68c2011-05-27 16:35:59 -0700101static int gen_74x164_direction_output(struct gpio_chip *gc,
102 unsigned offset, int val)
103{
104 gen_74x164_set_value(gc, offset, val);
105 return 0;
106}
107
Bill Pemberton38363092012-11-19 13:22:34 -0500108static int gen_74x164_probe(struct spi_device *spi)
Miguel Gaioead6db082010-10-27 15:33:18 -0700109{
110 struct gen_74x164_chip *chip;
111 struct gen_74x164_chip_platform_data *pdata;
112 int ret;
113
Maxime Ripard20bc4d52012-09-10 22:35:39 +0200114 if (!spi->dev.of_node) {
115 dev_err(&spi->dev, "No device tree data available.\n");
116 return -EINVAL;
117 }
118
Miguel Gaioead6db082010-10-27 15:33:18 -0700119 /*
120 * bits_per_word cannot be configured in platform data
121 */
122 spi->bits_per_word = 8;
123
124 ret = spi_setup(spi);
125 if (ret < 0)
126 return ret;
127
Maxime Ripard72eac302012-09-05 10:40:51 +0200128 chip = devm_kzalloc(&spi->dev, sizeof(*chip), GFP_KERNEL);
Miguel Gaioead6db082010-10-27 15:33:18 -0700129 if (!chip)
130 return -ENOMEM;
131
Jingoo Hane56aee12013-07-30 17:08:05 +0900132 pdata = dev_get_platdata(&spi->dev);
Maxime Ripard061505f2012-09-07 14:18:12 +0200133 if (pdata && pdata->base)
134 chip->gpio_chip.base = pdata->base;
135 else
136 chip->gpio_chip.base = -1;
137
Miguel Gaioead6db082010-10-27 15:33:18 -0700138 mutex_init(&chip->lock);
139
Jingoo Han6c0cf422013-03-15 18:17:18 +0900140 spi_set_drvdata(spi, chip);
Miguel Gaioead6db082010-10-27 15:33:18 -0700141
142 chip->spi = spi;
143
H Hartley Sweetena3cc68c2011-05-27 16:35:59 -0700144 chip->gpio_chip.label = spi->modalias;
145 chip->gpio_chip.direction_output = gen_74x164_direction_output;
Miguel Gaioead6db082010-10-27 15:33:18 -0700146 chip->gpio_chip.get = gen_74x164_get_value;
147 chip->gpio_chip.set = gen_74x164_set_value;
Maxime Ripard20bc4d52012-09-10 22:35:39 +0200148
149 if (of_property_read_u32(spi->dev.of_node, "registers-number", &chip->registers)) {
150 dev_err(&spi->dev, "Missing registers-number property in the DT.\n");
151 ret = -EINVAL;
152 goto exit_destroy;
153 }
154
155 chip->gpio_chip.ngpio = GEN_74X164_NUMBER_GPIOS * chip->registers;
Roland Stiggea48221a2012-10-16 15:24:01 +0200156 chip->buffer = devm_kzalloc(&spi->dev, chip->registers, GFP_KERNEL);
Maxime Ripard20bc4d52012-09-10 22:35:39 +0200157 if (!chip->buffer) {
158 ret = -ENOMEM;
159 goto exit_destroy;
160 }
161
Miguel Gaioead6db082010-10-27 15:33:18 -0700162 chip->gpio_chip.can_sleep = 1;
163 chip->gpio_chip.dev = &spi->dev;
164 chip->gpio_chip.owner = THIS_MODULE;
165
166 ret = __gen_74x164_write_config(chip);
167 if (ret) {
168 dev_err(&spi->dev, "Failed writing: %d\n", ret);
169 goto exit_destroy;
170 }
171
172 ret = gpiochip_add(&chip->gpio_chip);
173 if (ret)
174 goto exit_destroy;
175
176 return ret;
177
178exit_destroy:
Jingoo Han6c0cf422013-03-15 18:17:18 +0900179 spi_set_drvdata(spi, NULL);
Miguel Gaioead6db082010-10-27 15:33:18 -0700180 mutex_destroy(&chip->lock);
Miguel Gaioead6db082010-10-27 15:33:18 -0700181 return ret;
182}
183
Bill Pemberton206210c2012-11-19 13:25:50 -0500184static int gen_74x164_remove(struct spi_device *spi)
Miguel Gaioead6db082010-10-27 15:33:18 -0700185{
186 struct gen_74x164_chip *chip;
187 int ret;
188
Jingoo Han6c0cf422013-03-15 18:17:18 +0900189 chip = spi_get_drvdata(spi);
Miguel Gaioead6db082010-10-27 15:33:18 -0700190 if (chip == NULL)
191 return -ENODEV;
192
Jingoo Han6c0cf422013-03-15 18:17:18 +0900193 spi_set_drvdata(spi, NULL);
Miguel Gaioead6db082010-10-27 15:33:18 -0700194
195 ret = gpiochip_remove(&chip->gpio_chip);
Maxime Ripard72eac302012-09-05 10:40:51 +0200196 if (!ret)
Miguel Gaioead6db082010-10-27 15:33:18 -0700197 mutex_destroy(&chip->lock);
Maxime Ripard72eac302012-09-05 10:40:51 +0200198 else
Miguel Gaioead6db082010-10-27 15:33:18 -0700199 dev_err(&spi->dev, "Failed to remove the GPIO controller: %d\n",
200 ret);
201
202 return ret;
203}
204
Maxime Ripard0a90a9f2012-09-07 14:18:13 +0200205static const struct of_device_id gen_74x164_dt_ids[] = {
206 { .compatible = "fairchild,74hc595" },
207 {},
208};
209MODULE_DEVICE_TABLE(of, gen_74x164_dt_ids);
210
Miguel Gaioead6db082010-10-27 15:33:18 -0700211static struct spi_driver gen_74x164_driver = {
212 .driver = {
H Hartley Sweetena3cc68c2011-05-27 16:35:59 -0700213 .name = "74x164",
Miguel Gaioead6db082010-10-27 15:33:18 -0700214 .owner = THIS_MODULE,
Maxime Ripard0a90a9f2012-09-07 14:18:13 +0200215 .of_match_table = of_match_ptr(gen_74x164_dt_ids),
Miguel Gaioead6db082010-10-27 15:33:18 -0700216 },
217 .probe = gen_74x164_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500218 .remove = gen_74x164_remove,
Miguel Gaioead6db082010-10-27 15:33:18 -0700219};
Maxime Ripardab3b8782012-09-05 10:40:50 +0200220module_spi_driver(gen_74x164_driver);
Miguel Gaioead6db082010-10-27 15:33:18 -0700221
222MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
223MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
224MODULE_DESCRIPTION("GPIO expander driver for 74X164 8-bits shift register");
225MODULE_LICENSE("GPL v2");