Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6-block.git] / drivers / gpio / gpio-max7301.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
e952805d 2/*
0c36ec31
JB
3 * Copyright (C) 2006 Juergen Beisert, Pengutronix
4 * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix
e952805d 5 * Copyright (C) 2009 Wolfram Sang, Pengutronix
0c36ec31 6 *
e952805d 7 * Check max730x.c for further details.
0c36ec31
JB
8 */
9
e952805d 10#include <linux/module.h>
0c36ec31
JB
11#include <linux/init.h>
12#include <linux/platform_device.h>
13#include <linux/mutex.h>
5a0e3ad6 14#include <linux/slab.h>
0c36ec31
JB
15#include <linux/spi/spi.h>
16#include <linux/spi/max7301.h>
0c36ec31 17
e952805d
WS
18/* A write to the MAX7301 means one message with one transfer */
19static int max7301_spi_write(struct device *dev, unsigned int reg,
20 unsigned int val)
0c36ec31 21{
e952805d 22 struct spi_device *spi = to_spi_device(dev);
0c36ec31 23 u16 word = ((reg & 0x7F) << 8) | (val & 0xFF);
e952805d 24
abf221d2 25 return spi_write_then_read(spi, &word, sizeof(word), NULL, 0);
0c36ec31
JB
26}
27
e952805d
WS
28/* A read from the MAX7301 means two transfers; here, one message each */
29
30static int max7301_spi_read(struct device *dev, unsigned int reg)
0c36ec31
JB
31{
32 int ret;
33 u16 word;
e952805d 34 struct spi_device *spi = to_spi_device(dev);
0c36ec31
JB
35
36 word = 0x8000 | (reg << 8);
abf221d2
CL
37 ret = spi_write_then_read(spi, &word, sizeof(word), &word,
38 sizeof(word));
0c36ec31
JB
39 if (ret)
40 return ret;
41 return word & 0xff;
42}
43
3836309d 44static int max7301_probe(struct spi_device *spi)
0c36ec31
JB
45{
46 struct max7301 *ts;
e952805d 47 int ret;
0c36ec31 48
e952805d 49 /* bits_per_word cannot be configured in platform data */
31eb4b55 50 spi->bits_per_word = 16;
0c36ec31
JB
51 ret = spi_setup(spi);
52 if (ret < 0)
53 return ret;
54
4cb06cd5 55 ts = devm_kzalloc(&spi->dev, sizeof(struct max7301), GFP_KERNEL);
0c36ec31
JB
56 if (!ts)
57 return -ENOMEM;
58
e952805d
WS
59 ts->read = max7301_spi_read;
60 ts->write = max7301_spi_write;
61 ts->dev = &spi->dev;
0c36ec31 62
e952805d 63 ret = __max730x_probe(ts);
0c36ec31
JB
64 return ret;
65}
66
206210ce 67static int max7301_remove(struct spi_device *spi)
0c36ec31 68{
e952805d 69 return __max730x_remove(&spi->dev);
0c36ec31
JB
70}
71
e952805d
WS
72static const struct spi_device_id max7301_id[] = {
73 { "max7301", 0 },
74 { }
75};
76MODULE_DEVICE_TABLE(spi, max7301_id);
77
0c36ec31
JB
78static struct spi_driver max7301_driver = {
79 .driver = {
e952805d 80 .name = "max7301",
0c36ec31 81 },
e952805d 82 .probe = max7301_probe,
8283c4ff 83 .remove = max7301_remove,
e952805d 84 .id_table = max7301_id,
0c36ec31
JB
85};
86
87static int __init max7301_init(void)
88{
89 return spi_register_driver(&max7301_driver);
90}
673c0c00
DB
91/* register after spi postcore initcall and before
92 * subsys initcalls that may rely on these GPIOs
93 */
94subsys_initcall(max7301_init);
0c36ec31
JB
95
96static void __exit max7301_exit(void)
97{
98 spi_unregister_driver(&max7301_driver);
99}
0c36ec31
JB
100module_exit(max7301_exit);
101
e952805d 102MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
0c36ec31 103MODULE_LICENSE("GPL v2");
e952805d 104MODULE_DESCRIPTION("MAX7301 GPIO-Expander");