Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 2 | /* |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 3 | * Copyright (C) 2006 Juergen Beisert, Pengutronix |
| 4 | * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 5 | * Copyright (C) 2009 Wolfram Sang, Pengutronix |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 6 | * |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 7 | * Check max730x.c for further details. |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 10 | #include <linux/module.h> |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 11 | #include <linux/init.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 15 | #include <linux/spi/spi.h> |
| 16 | #include <linux/spi/max7301.h> |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 17 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 18 | /* A write to the MAX7301 means one message with one transfer */ |
| 19 | static int max7301_spi_write(struct device *dev, unsigned int reg, |
| 20 | unsigned int val) |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 21 | { |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 22 | struct spi_device *spi = to_spi_device(dev); |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 23 | u16 word = ((reg & 0x7F) << 8) | (val & 0xFF); |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 24 | |
Christophe Leroy | abf221d | 2018-12-07 13:07:55 +0000 | [diff] [blame] | 25 | return spi_write_then_read(spi, &word, sizeof(word), NULL, 0); |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 28 | /* A read from the MAX7301 means two transfers; here, one message each */ |
| 29 | |
| 30 | static int max7301_spi_read(struct device *dev, unsigned int reg) |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 31 | { |
| 32 | int ret; |
| 33 | u16 word; |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 34 | struct spi_device *spi = to_spi_device(dev); |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 35 | |
| 36 | word = 0x8000 | (reg << 8); |
Christophe Leroy | abf221d | 2018-12-07 13:07:55 +0000 | [diff] [blame] | 37 | ret = spi_write_then_read(spi, &word, sizeof(word), &word, |
| 38 | sizeof(word)); |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 39 | if (ret) |
| 40 | return ret; |
| 41 | return word & 0xff; |
| 42 | } |
| 43 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 44 | static int max7301_probe(struct spi_device *spi) |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 45 | { |
| 46 | struct max7301 *ts; |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 47 | int ret; |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 48 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 49 | /* bits_per_word cannot be configured in platform data */ |
Christophe Leroy | 31eb4b55 | 2013-08-20 08:29:23 +0200 | [diff] [blame] | 50 | spi->bits_per_word = 16; |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 51 | ret = spi_setup(spi); |
| 52 | if (ret < 0) |
| 53 | return ret; |
| 54 | |
Jingoo Han | 4cb06cd | 2013-03-15 18:15:06 +0900 | [diff] [blame] | 55 | ts = devm_kzalloc(&spi->dev, sizeof(struct max7301), GFP_KERNEL); |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 56 | if (!ts) |
| 57 | return -ENOMEM; |
| 58 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 59 | ts->read = max7301_spi_read; |
| 60 | ts->write = max7301_spi_write; |
| 61 | ts->dev = &spi->dev; |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 62 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 63 | ret = __max730x_probe(ts); |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 64 | return ret; |
| 65 | } |
| 66 | |
Bill Pemberton | 206210c | 2012-11-19 13:25:50 -0500 | [diff] [blame] | 67 | static int max7301_remove(struct spi_device *spi) |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 68 | { |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 69 | return __max730x_remove(&spi->dev); |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 72 | static const struct spi_device_id max7301_id[] = { |
| 73 | { "max7301", 0 }, |
| 74 | { } |
| 75 | }; |
| 76 | MODULE_DEVICE_TABLE(spi, max7301_id); |
| 77 | |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 78 | static struct spi_driver max7301_driver = { |
| 79 | .driver = { |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 80 | .name = "max7301", |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 81 | }, |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 82 | .probe = max7301_probe, |
Bill Pemberton | 8283c4f | 2012-11-19 13:20:08 -0500 | [diff] [blame] | 83 | .remove = max7301_remove, |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 84 | .id_table = max7301_id, |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static int __init max7301_init(void) |
| 88 | { |
| 89 | return spi_register_driver(&max7301_driver); |
| 90 | } |
David Brownell | 673c0c0 | 2008-10-15 22:02:46 -0700 | [diff] [blame] | 91 | /* register after spi postcore initcall and before |
| 92 | * subsys initcalls that may rely on these GPIOs |
| 93 | */ |
| 94 | subsys_initcall(max7301_init); |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 95 | |
| 96 | static void __exit max7301_exit(void) |
| 97 | { |
| 98 | spi_unregister_driver(&max7301_driver); |
| 99 | } |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 100 | module_exit(max7301_exit); |
| 101 | |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 102 | MODULE_AUTHOR("Juergen Beisert, Wolfram Sang"); |
Juergen Beisert | 0c36ec3 | 2008-07-21 14:21:34 -0700 | [diff] [blame] | 103 | MODULE_LICENSE("GPL v2"); |
Wolfram Sang | e952805 | 2010-03-05 13:44:33 -0800 | [diff] [blame] | 104 | MODULE_DESCRIPTION("MAX7301 GPIO-Expander"); |