1 // SPDX-License-Identifier: GPL-2.0+
3 * TQ-Systems PLD MFD core driver, based on vendor driver by
4 * Vadim V.Vlasov <vvlasov@dev.rtsoft.ru>
6 * Copyright (c) 2015 TQ-Systems GmbH
7 * Copyright (c) 2019 Andrew Lunn <andrew@lunn.ch>
10 #include <linux/delay.h>
11 #include <linux/dmi.h>
12 #include <linux/i2c.h>
14 #include <linux/mfd/core.h>
15 #include <linux/module.h>
16 #include <linux/platform_data/i2c-ocores.h>
17 #include <linux/platform_device.h>
19 #define TQMX86_IOBASE 0x180
20 #define TQMX86_IOSIZE 0x20
21 #define TQMX86_IOBASE_I2C 0x1a0
22 #define TQMX86_IOSIZE_I2C 0xa
23 #define TQMX86_IOBASE_WATCHDOG 0x18b
24 #define TQMX86_IOSIZE_WATCHDOG 0x2
25 #define TQMX86_IOBASE_GPIO 0x18d
26 #define TQMX86_IOSIZE_GPIO 0x4
28 #define TQMX86_REG_BOARD_ID 0x00
29 #define TQMX86_REG_BOARD_ID_E38M 1
30 #define TQMX86_REG_BOARD_ID_50UC 2
31 #define TQMX86_REG_BOARD_ID_E38C 3
32 #define TQMX86_REG_BOARD_ID_60EB 4
33 #define TQMX86_REG_BOARD_ID_E39MS 5
34 #define TQMX86_REG_BOARD_ID_E39C1 6
35 #define TQMX86_REG_BOARD_ID_E39C2 7
36 #define TQMX86_REG_BOARD_ID_70EB 8
37 #define TQMX86_REG_BOARD_ID_80UC 9
38 #define TQMX86_REG_BOARD_ID_120UC 10
39 #define TQMX86_REG_BOARD_ID_110EB 11
40 #define TQMX86_REG_BOARD_ID_E40M 12
41 #define TQMX86_REG_BOARD_ID_E40S 13
42 #define TQMX86_REG_BOARD_ID_E40C1 14
43 #define TQMX86_REG_BOARD_ID_E40C2 15
44 #define TQMX86_REG_BOARD_ID_130UC 16
45 #define TQMX86_REG_BOARD_ID_E41S 19
46 #define TQMX86_REG_BOARD_REV 0x01
47 #define TQMX86_REG_IO_EXT_INT 0x06
48 #define TQMX86_REG_IO_EXT_INT_NONE 0
49 #define TQMX86_REG_IO_EXT_INT_7 1
50 #define TQMX86_REG_IO_EXT_INT_9 2
51 #define TQMX86_REG_IO_EXT_INT_12 3
52 #define TQMX86_REG_IO_EXT_INT_MASK 0x3
53 #define TQMX86_REG_IO_EXT_INT_I2C1_SHIFT 0
54 #define TQMX86_REG_IO_EXT_INT_GPIO_SHIFT 4
55 #define TQMX86_REG_SAUC 0x17
57 #define TQMX86_REG_I2C_DETECT 0x1a7
58 #define TQMX86_REG_I2C_DETECT_SOFT 0xa5
61 module_param(gpio_irq
, uint
, 0);
62 MODULE_PARM_DESC(gpio_irq
, "GPIO IRQ number (valid parameters: 7, 9, 12)");
65 module_param(i2c1_irq
, uint
, 0);
66 MODULE_PARM_DESC(i2c1_irq
, "I2C1 IRQ number (valid parameters: 7, 9, 12)");
68 enum tqmx86_i2c1_resource_type
{
73 static struct resource tqmx_i2c_soft_resources
[] = {
74 [TQMX86_I2C1_IO
] = DEFINE_RES_IO(TQMX86_IOBASE_I2C
, TQMX86_IOSIZE_I2C
),
75 /* Placeholder for IRQ resource */
76 [TQMX86_I2C1_IRQ
] = {},
79 static const struct resource tqmx_watchdog_resources
[] = {
80 DEFINE_RES_IO(TQMX86_IOBASE_WATCHDOG
, TQMX86_IOSIZE_WATCHDOG
),
83 enum tqmx86_gpio_resource_type
{
88 static struct resource tqmx_gpio_resources
[] = {
89 [TQMX86_GPIO_IO
] = DEFINE_RES_IO(TQMX86_IOBASE_GPIO
, TQMX86_IOSIZE_GPIO
),
90 /* Placeholder for IRQ resource */
91 [TQMX86_GPIO_IRQ
] = {},
94 static struct i2c_board_info tqmx86_i2c_devices
[] = {
96 /* 4K EEPROM at 0x50 */
97 I2C_BOARD_INFO("24c32", 0x50),
101 static struct ocores_i2c_platform_data ocores_platform_data
= {
102 .num_devices
= ARRAY_SIZE(tqmx86_i2c_devices
),
103 .devices
= tqmx86_i2c_devices
,
106 static const struct mfd_cell tqmx86_i2c_soft_dev
[] = {
108 .name
= "ocores-i2c",
109 .platform_data
= &ocores_platform_data
,
110 .pdata_size
= sizeof(ocores_platform_data
),
111 .resources
= tqmx_i2c_soft_resources
,
112 .num_resources
= ARRAY_SIZE(tqmx_i2c_soft_resources
),
116 static const struct mfd_cell tqmx86_devs
[] = {
118 .name
= "tqmx86-wdt",
119 .resources
= tqmx_watchdog_resources
,
120 .num_resources
= ARRAY_SIZE(tqmx_watchdog_resources
),
121 .ignore_resource_conflicts
= true,
124 .name
= "tqmx86-gpio",
125 .resources
= tqmx_gpio_resources
,
126 .num_resources
= ARRAY_SIZE(tqmx_gpio_resources
),
127 .ignore_resource_conflicts
= true,
131 static const char *tqmx86_board_id_to_name(u8 board_id
, u8 sauc
)
134 case TQMX86_REG_BOARD_ID_E38M
:
136 case TQMX86_REG_BOARD_ID_50UC
:
138 case TQMX86_REG_BOARD_ID_E38C
:
140 case TQMX86_REG_BOARD_ID_60EB
:
142 case TQMX86_REG_BOARD_ID_E39MS
:
143 return (sauc
== 0xff) ? "TQMxE39M" : "TQMxE39S";
144 case TQMX86_REG_BOARD_ID_E39C1
:
146 case TQMX86_REG_BOARD_ID_E39C2
:
148 case TQMX86_REG_BOARD_ID_70EB
:
150 case TQMX86_REG_BOARD_ID_80UC
:
152 case TQMX86_REG_BOARD_ID_120UC
:
154 case TQMX86_REG_BOARD_ID_110EB
:
156 case TQMX86_REG_BOARD_ID_E40M
:
158 case TQMX86_REG_BOARD_ID_E40S
:
160 case TQMX86_REG_BOARD_ID_E40C1
:
162 case TQMX86_REG_BOARD_ID_E40C2
:
164 case TQMX86_REG_BOARD_ID_130UC
:
166 case TQMX86_REG_BOARD_ID_E41S
:
173 static int tqmx86_board_id_to_clk_rate(struct device
*dev
, u8 board_id
)
176 case TQMX86_REG_BOARD_ID_50UC
:
177 case TQMX86_REG_BOARD_ID_60EB
:
178 case TQMX86_REG_BOARD_ID_70EB
:
179 case TQMX86_REG_BOARD_ID_80UC
:
180 case TQMX86_REG_BOARD_ID_120UC
:
181 case TQMX86_REG_BOARD_ID_110EB
:
182 case TQMX86_REG_BOARD_ID_E40M
:
183 case TQMX86_REG_BOARD_ID_E40S
:
184 case TQMX86_REG_BOARD_ID_E40C1
:
185 case TQMX86_REG_BOARD_ID_E40C2
:
186 case TQMX86_REG_BOARD_ID_130UC
:
187 case TQMX86_REG_BOARD_ID_E41S
:
189 case TQMX86_REG_BOARD_ID_E39MS
:
190 case TQMX86_REG_BOARD_ID_E39C1
:
191 case TQMX86_REG_BOARD_ID_E39C2
:
193 case TQMX86_REG_BOARD_ID_E38M
:
194 case TQMX86_REG_BOARD_ID_E38C
:
197 dev_warn(dev
, "unknown board %d, assuming 24MHz LPC clock\n",
203 static int tqmx86_setup_irq(struct device
*dev
, const char *label
, u8 irq
,
204 void __iomem
*io_base
, u8 reg_shift
)
211 irq_cfg
= TQMX86_REG_IO_EXT_INT_NONE
;
214 irq_cfg
= TQMX86_REG_IO_EXT_INT_7
;
217 irq_cfg
= TQMX86_REG_IO_EXT_INT_9
;
220 irq_cfg
= TQMX86_REG_IO_EXT_INT_12
;
223 dev_err(dev
, "invalid %s IRQ (%d)\n", label
, irq
);
227 val
= ioread8(io_base
+ TQMX86_REG_IO_EXT_INT
);
228 val
&= ~(TQMX86_REG_IO_EXT_INT_MASK
<< reg_shift
);
229 val
|= (irq_cfg
& TQMX86_REG_IO_EXT_INT_MASK
) << reg_shift
;
231 iowrite8(val
, io_base
+ TQMX86_REG_IO_EXT_INT
);
232 readback
= ioread8(io_base
+ TQMX86_REG_IO_EXT_INT
);
233 if (readback
!= val
) {
234 dev_warn(dev
, "%s interrupts not supported\n", label
);
241 static int tqmx86_probe(struct platform_device
*pdev
)
243 u8 board_id
, sauc
, rev
, i2c_det
;
244 struct device
*dev
= &pdev
->dev
;
245 const char *board_name
;
246 void __iomem
*io_base
;
249 io_base
= devm_ioport_map(dev
, TQMX86_IOBASE
, TQMX86_IOSIZE
);
253 board_id
= ioread8(io_base
+ TQMX86_REG_BOARD_ID
);
254 sauc
= ioread8(io_base
+ TQMX86_REG_SAUC
);
255 board_name
= tqmx86_board_id_to_name(board_id
, sauc
);
256 rev
= ioread8(io_base
+ TQMX86_REG_BOARD_REV
);
259 "Found %s - Board ID %d, PCB Revision %d, PLD Revision %d\n",
260 board_name
, board_id
, rev
>> 4, rev
& 0xf);
263 * The I2C_DETECT register is in the range assigned to the I2C driver
264 * later, so we don't extend TQMX86_IOSIZE. Use inb() for this one-off
265 * access instead of ioport_map + unmap.
267 i2c_det
= inb(TQMX86_REG_I2C_DETECT
);
270 err
= tqmx86_setup_irq(dev
, "GPIO", gpio_irq
, io_base
,
271 TQMX86_REG_IO_EXT_INT_GPIO_SHIFT
);
273 tqmx_gpio_resources
[TQMX86_GPIO_IRQ
] = DEFINE_RES_IRQ(gpio_irq
);
276 ocores_platform_data
.clock_khz
= tqmx86_board_id_to_clk_rate(dev
, board_id
);
278 if (i2c_det
== TQMX86_REG_I2C_DETECT_SOFT
) {
280 err
= tqmx86_setup_irq(dev
, "I2C1", i2c1_irq
, io_base
,
281 TQMX86_REG_IO_EXT_INT_I2C1_SHIFT
);
283 tqmx_i2c_soft_resources
[TQMX86_I2C1_IRQ
] = DEFINE_RES_IRQ(i2c1_irq
);
286 err
= devm_mfd_add_devices(dev
, PLATFORM_DEVID_NONE
,
288 ARRAY_SIZE(tqmx86_i2c_soft_dev
),
294 return devm_mfd_add_devices(dev
, PLATFORM_DEVID_NONE
,
296 ARRAY_SIZE(tqmx86_devs
),
300 static int tqmx86_create_platform_device(const struct dmi_system_id
*id
)
302 struct platform_device
*pdev
;
305 pdev
= platform_device_alloc("tqmx86", -1);
309 err
= platform_device_add(pdev
);
311 platform_device_put(pdev
);
316 static const struct dmi_system_id tqmx86_dmi_table
[] __initconst
= {
320 DMI_MATCH(DMI_SYS_VENDOR
, "TQ-Group"),
321 DMI_MATCH(DMI_PRODUCT_NAME
, "TQMx"),
323 .callback
= tqmx86_create_platform_device
,
328 DMI_MATCH(DMI_SYS_VENDOR
, "TQ-Systems"),
329 DMI_MATCH(DMI_PRODUCT_NAME
, "TQMx"),
331 .callback
= tqmx86_create_platform_device
,
335 MODULE_DEVICE_TABLE(dmi
, tqmx86_dmi_table
);
337 static struct platform_driver tqmx86_driver
= {
341 .probe
= tqmx86_probe
,
344 static int __init
tqmx86_init(void)
346 if (!dmi_check_system(tqmx86_dmi_table
))
349 return platform_driver_register(&tqmx86_driver
);
352 module_init(tqmx86_init
);
354 MODULE_DESCRIPTION("TQMx86 PLD Core Driver");
355 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
356 MODULE_LICENSE("GPL");
357 MODULE_ALIAS("platform:tqmx86");