xref: /openbmc/linux/drivers/mfd/da9052-spi.c (revision bc33f5e5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * SPI access for Dialog DA9052 PMICs.
4  *
5  * Copyright(c) 2011 Dialog Semiconductor Ltd.
6  *
7  * Author: David Dajun Chen <dchen@diasemi.com>
8  */
9 
10 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/input.h>
13 #include <linux/mfd/core.h>
14 #include <linux/spi/spi.h>
15 #include <linux/err.h>
16 
17 #include <linux/mfd/da9052/da9052.h>
18 
19 static int da9052_spi_probe(struct spi_device *spi)
20 {
21 	struct regmap_config config;
22 	int ret;
23 	const struct spi_device_id *id = spi_get_device_id(spi);
24 	struct da9052 *da9052;
25 
26 	da9052 = devm_kzalloc(&spi->dev, sizeof(struct da9052), GFP_KERNEL);
27 	if (!da9052)
28 		return -ENOMEM;
29 
30 	spi->mode = SPI_MODE_0;
31 	spi->bits_per_word = 8;
32 	spi_setup(spi);
33 
34 	da9052->dev = &spi->dev;
35 	da9052->chip_irq = spi->irq;
36 
37 	spi_set_drvdata(spi, da9052);
38 
39 	config = da9052_regmap_config;
40 	config.read_flag_mask = 1;
41 	config.reg_bits = 7;
42 	config.pad_bits = 1;
43 	config.val_bits = 8;
44 	config.use_single_read = true;
45 	config.use_single_write = true;
46 
47 	da9052->regmap = devm_regmap_init_spi(spi, &config);
48 	if (IS_ERR(da9052->regmap)) {
49 		ret = PTR_ERR(da9052->regmap);
50 		dev_err(&spi->dev, "Failed to allocate register map: %d\n",
51 			ret);
52 		return ret;
53 	}
54 
55 	return da9052_device_init(da9052, id->driver_data);
56 }
57 
58 static void da9052_spi_remove(struct spi_device *spi)
59 {
60 	struct da9052 *da9052 = spi_get_drvdata(spi);
61 
62 	da9052_device_exit(da9052);
63 }
64 
65 static const struct spi_device_id da9052_spi_id[] = {
66 	{"da9052", DA9052},
67 	{"da9053-aa", DA9053_AA},
68 	{"da9053-ba", DA9053_BA},
69 	{"da9053-bb", DA9053_BB},
70 	{"da9053-bc", DA9053_BC},
71 	{}
72 };
73 
74 static struct spi_driver da9052_spi_driver = {
75 	.probe = da9052_spi_probe,
76 	.remove = da9052_spi_remove,
77 	.id_table = da9052_spi_id,
78 	.driver = {
79 		.name = "da9052",
80 	},
81 };
82 
83 static int __init da9052_spi_init(void)
84 {
85 	int ret;
86 
87 	ret = spi_register_driver(&da9052_spi_driver);
88 	if (ret != 0) {
89 		pr_err("Failed to register DA9052 SPI driver, %d\n", ret);
90 		return ret;
91 	}
92 
93 	return 0;
94 }
95 subsys_initcall(da9052_spi_init);
96 
97 static void __exit da9052_spi_exit(void)
98 {
99 	spi_unregister_driver(&da9052_spi_driver);
100 }
101 module_exit(da9052_spi_exit);
102 
103 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
104 MODULE_DESCRIPTION("SPI driver for Dialog DA9052 PMIC");
105 MODULE_LICENSE("GPL");
106