blob: 1307c243b4e9292f0304800bead53a18a686500c [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Wolfram Sange9528052010-03-05 13:44:33 -08002/*
Juergen Beisert0c36ec32008-07-21 14:21:34 -07003 * Copyright (C) 2006 Juergen Beisert, Pengutronix
4 * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix
Wolfram Sange9528052010-03-05 13:44:33 -08005 * Copyright (C) 2009 Wolfram Sang, Pengutronix
Juergen Beisert0c36ec32008-07-21 14:21:34 -07006 *
Wolfram Sange9528052010-03-05 13:44:33 -08007 * Check max730x.c for further details.
Juergen Beisert0c36ec32008-07-21 14:21:34 -07008 */
9
Wolfram Sange9528052010-03-05 13:44:33 -080010#include <linux/module.h>
Juergen Beisert0c36ec32008-07-21 14:21:34 -070011#include <linux/init.h>
12#include <linux/platform_device.h>
13#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Juergen Beisert0c36ec32008-07-21 14:21:34 -070015#include <linux/spi/spi.h>
16#include <linux/spi/max7301.h>
Juergen Beisert0c36ec32008-07-21 14:21:34 -070017
Wolfram Sange9528052010-03-05 13:44:33 -080018/* 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)
Juergen Beisert0c36ec32008-07-21 14:21:34 -070021{
Wolfram Sange9528052010-03-05 13:44:33 -080022 struct spi_device *spi = to_spi_device(dev);
Juergen Beisert0c36ec32008-07-21 14:21:34 -070023 u16 word = ((reg & 0x7F) << 8) | (val & 0xFF);
Wolfram Sange9528052010-03-05 13:44:33 -080024
Christophe Leroyabf221d2018-12-07 13:07:55 +000025 return spi_write_then_read(spi, &word, sizeof(word), NULL, 0);
Juergen Beisert0c36ec32008-07-21 14:21:34 -070026}
27
Wolfram Sange9528052010-03-05 13:44:33 -080028/* A read from the MAX7301 means two transfers; here, one message each */
29
30static int max7301_spi_read(struct device *dev, unsigned int reg)
Juergen Beisert0c36ec32008-07-21 14:21:34 -070031{
32 int ret;
33 u16 word;
Wolfram Sange9528052010-03-05 13:44:33 -080034 struct spi_device *spi = to_spi_device(dev);
Juergen Beisert0c36ec32008-07-21 14:21:34 -070035
36 word = 0x8000 | (reg << 8);
Christophe Leroyabf221d2018-12-07 13:07:55 +000037 ret = spi_write_then_read(spi, &word, sizeof(word), &word,
38 sizeof(word));
Juergen Beisert0c36ec32008-07-21 14:21:34 -070039 if (ret)
40 return ret;
41 return word & 0xff;
42}
43
Bill Pemberton38363092012-11-19 13:22:34 -050044static int max7301_probe(struct spi_device *spi)
Juergen Beisert0c36ec32008-07-21 14:21:34 -070045{
46 struct max7301 *ts;
Wolfram Sange9528052010-03-05 13:44:33 -080047 int ret;
Juergen Beisert0c36ec32008-07-21 14:21:34 -070048
Wolfram Sange9528052010-03-05 13:44:33 -080049 /* bits_per_word cannot be configured in platform data */
Christophe Leroy31eb4b552013-08-20 08:29:23 +020050 spi->bits_per_word = 16;
Juergen Beisert0c36ec32008-07-21 14:21:34 -070051 ret = spi_setup(spi);
52 if (ret < 0)
53 return ret;
54
Jingoo Han4cb06cd2013-03-15 18:15:06 +090055 ts = devm_kzalloc(&spi->dev, sizeof(struct max7301), GFP_KERNEL);
Juergen Beisert0c36ec32008-07-21 14:21:34 -070056 if (!ts)
57 return -ENOMEM;
58
Wolfram Sange9528052010-03-05 13:44:33 -080059 ts->read = max7301_spi_read;
60 ts->write = max7301_spi_write;
61 ts->dev = &spi->dev;
Juergen Beisert0c36ec32008-07-21 14:21:34 -070062
Wolfram Sange9528052010-03-05 13:44:33 -080063 ret = __max730x_probe(ts);
Juergen Beisert0c36ec32008-07-21 14:21:34 -070064 return ret;
65}
66
Bill Pemberton206210c2012-11-19 13:25:50 -050067static int max7301_remove(struct spi_device *spi)
Juergen Beisert0c36ec32008-07-21 14:21:34 -070068{
Wolfram Sange9528052010-03-05 13:44:33 -080069 return __max730x_remove(&spi->dev);
Juergen Beisert0c36ec32008-07-21 14:21:34 -070070}
71
Wolfram Sange9528052010-03-05 13:44:33 -080072static const struct spi_device_id max7301_id[] = {
73 { "max7301", 0 },
74 { }
75};
76MODULE_DEVICE_TABLE(spi, max7301_id);
77
Juergen Beisert0c36ec32008-07-21 14:21:34 -070078static struct spi_driver max7301_driver = {
79 .driver = {
Wolfram Sange9528052010-03-05 13:44:33 -080080 .name = "max7301",
Juergen Beisert0c36ec32008-07-21 14:21:34 -070081 },
Wolfram Sange9528052010-03-05 13:44:33 -080082 .probe = max7301_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -050083 .remove = max7301_remove,
Wolfram Sange9528052010-03-05 13:44:33 -080084 .id_table = max7301_id,
Juergen Beisert0c36ec32008-07-21 14:21:34 -070085};
86
87static int __init max7301_init(void)
88{
89 return spi_register_driver(&max7301_driver);
90}
David Brownell673c0c02008-10-15 22:02:46 -070091/* register after spi postcore initcall and before
92 * subsys initcalls that may rely on these GPIOs
93 */
94subsys_initcall(max7301_init);
Juergen Beisert0c36ec32008-07-21 14:21:34 -070095
96static void __exit max7301_exit(void)
97{
98 spi_unregister_driver(&max7301_driver);
99}
Juergen Beisert0c36ec32008-07-21 14:21:34 -0700100module_exit(max7301_exit);
101
Wolfram Sange9528052010-03-05 13:44:33 -0800102MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
Juergen Beisert0c36ec32008-07-21 14:21:34 -0700103MODULE_LICENSE("GPL v2");
Wolfram Sange9528052010-03-05 13:44:33 -0800104MODULE_DESCRIPTION("MAX7301 GPIO-Expander");