Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2006 Juergen Beisert, Pengutronix
0004  * Copyright (C) 2008 Guennadi Liakhovetski, Pengutronix
0005  * Copyright (C) 2009 Wolfram Sang, Pengutronix
0006  *
0007  * Check max730x.c for further details.
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/init.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/mutex.h>
0014 #include <linux/slab.h>
0015 #include <linux/spi/spi.h>
0016 #include <linux/spi/max7301.h>
0017 
0018 /* A write to the MAX7301 means one message with one transfer */
0019 static int max7301_spi_write(struct device *dev, unsigned int reg,
0020                 unsigned int val)
0021 {
0022     struct spi_device *spi = to_spi_device(dev);
0023     u16 word = ((reg & 0x7F) << 8) | (val & 0xFF);
0024 
0025     return spi_write_then_read(spi, &word, sizeof(word), NULL, 0);
0026 }
0027 
0028 /* A read from the MAX7301 means two transfers; here, one message each */
0029 
0030 static int max7301_spi_read(struct device *dev, unsigned int reg)
0031 {
0032     int ret;
0033     u16 word;
0034     struct spi_device *spi = to_spi_device(dev);
0035 
0036     word = 0x8000 | (reg << 8);
0037     ret = spi_write_then_read(spi, &word, sizeof(word), &word,
0038                   sizeof(word));
0039     if (ret)
0040         return ret;
0041     return word & 0xff;
0042 }
0043 
0044 static int max7301_probe(struct spi_device *spi)
0045 {
0046     struct max7301 *ts;
0047     int ret;
0048 
0049     /* bits_per_word cannot be configured in platform data */
0050     spi->bits_per_word = 16;
0051     ret = spi_setup(spi);
0052     if (ret < 0)
0053         return ret;
0054 
0055     ts = devm_kzalloc(&spi->dev, sizeof(struct max7301), GFP_KERNEL);
0056     if (!ts)
0057         return -ENOMEM;
0058 
0059     ts->read = max7301_spi_read;
0060     ts->write = max7301_spi_write;
0061     ts->dev = &spi->dev;
0062 
0063     ret = __max730x_probe(ts);
0064     return ret;
0065 }
0066 
0067 static void max7301_remove(struct spi_device *spi)
0068 {
0069     __max730x_remove(&spi->dev);
0070 }
0071 
0072 static const struct spi_device_id max7301_id[] = {
0073     { "max7301", 0 },
0074     { }
0075 };
0076 MODULE_DEVICE_TABLE(spi, max7301_id);
0077 
0078 static struct spi_driver max7301_driver = {
0079     .driver = {
0080         .name = "max7301",
0081     },
0082     .probe = max7301_probe,
0083     .remove = max7301_remove,
0084     .id_table = max7301_id,
0085 };
0086 
0087 static int __init max7301_init(void)
0088 {
0089     return spi_register_driver(&max7301_driver);
0090 }
0091 /* register after spi postcore initcall and before
0092  * subsys initcalls that may rely on these GPIOs
0093  */
0094 subsys_initcall(max7301_init);
0095 
0096 static void __exit max7301_exit(void)
0097 {
0098     spi_unregister_driver(&max7301_driver);
0099 }
0100 module_exit(max7301_exit);
0101 
0102 MODULE_AUTHOR("Juergen Beisert, Wolfram Sang");
0103 MODULE_LICENSE("GPL v2");
0104 MODULE_DESCRIPTION("MAX7301 GPIO-Expander");