Skip to content

Commit ebe508e

Browse files
andy-shevAndi Shyti
authored andcommitted
i2c: designware: Consolidate firmware parsing and configuring code
We have the same code flows in the PCI and platform drivers. Moreover, the flow requires the common code to export a few functions. Instead, consolidate that flow under new function called i2c_dw_fw_parse_and_configure() and drop unneeded exports. Reviewed-by: Andi Shyti <[email protected]> Reviewed-by: Mario Limonciello <[email protected]> Signed-off-by: Andy Shevchenko <[email protected]> Signed-off-by: Andi Shyti <[email protected]>
1 parent 628c248 commit ebe508e

File tree

4 files changed

+66
-70
lines changed

4 files changed

+66
-70
lines changed

drivers/i2c/busses/i2c-designware-common.c

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/io.h>
2121
#include <linux/kernel.h>
2222
#include <linux/module.h>
23+
#include <linux/of.h>
2324
#include <linux/pm_runtime.h>
2425
#include <linux/regmap.h>
2526
#include <linux/swab.h>
@@ -188,7 +189,7 @@ static const u32 supported_speeds[] = {
188189
I2C_MAX_STANDARD_MODE_FREQ,
189190
};
190191

191-
int i2c_dw_validate_speed(struct dw_i2c_dev *dev)
192+
static int i2c_dw_validate_speed(struct dw_i2c_dev *dev)
192193
{
193194
struct i2c_timings *t = &dev->timings;
194195
unsigned int i;
@@ -208,7 +209,44 @@ int i2c_dw_validate_speed(struct dw_i2c_dev *dev)
208209

209210
return -EINVAL;
210211
}
211-
EXPORT_SYMBOL_GPL(i2c_dw_validate_speed);
212+
213+
#ifdef CONFIG_OF
214+
215+
#include <linux/platform_device.h>
216+
217+
#define MSCC_ICPU_CFG_TWI_DELAY 0x0
218+
#define MSCC_ICPU_CFG_TWI_DELAY_ENABLE BIT(0)
219+
#define MSCC_ICPU_CFG_TWI_SPIKE_FILTER 0x4
220+
221+
static int mscc_twi_set_sda_hold_time(struct dw_i2c_dev *dev)
222+
{
223+
writel((dev->sda_hold_time << 1) | MSCC_ICPU_CFG_TWI_DELAY_ENABLE,
224+
dev->ext + MSCC_ICPU_CFG_TWI_DELAY);
225+
226+
return 0;
227+
}
228+
229+
static void i2c_dw_of_configure(struct device *device)
230+
{
231+
struct platform_device *pdev = to_platform_device(device);
232+
struct dw_i2c_dev *dev = dev_get_drvdata(device);
233+
234+
switch (dev->flags & MODEL_MASK) {
235+
case MODEL_MSCC_OCELOT:
236+
dev->ext = devm_platform_ioremap_resource(pdev, 1);
237+
if (!IS_ERR(dev->ext))
238+
dev->set_sda_hold_time = mscc_twi_set_sda_hold_time;
239+
break;
240+
default:
241+
break;
242+
}
243+
}
244+
245+
#else /* CONFIG_OF */
246+
247+
static inline void i2c_dw_of_configure(struct device *device) { }
248+
249+
#endif /* CONFIG_OF */
212250

213251
#ifdef CONFIG_ACPI
214252

@@ -255,7 +293,7 @@ static void i2c_dw_acpi_params(struct device *device, char method[],
255293
kfree(buf.pointer);
256294
}
257295

258-
void i2c_dw_acpi_configure(struct device *device)
296+
static void i2c_dw_acpi_configure(struct device *device)
259297
{
260298
struct dw_i2c_dev *dev = dev_get_drvdata(device);
261299
struct i2c_timings *t = &dev->timings;
@@ -286,7 +324,6 @@ void i2c_dw_acpi_configure(struct device *device)
286324
break;
287325
}
288326
}
289-
EXPORT_SYMBOL_GPL(i2c_dw_acpi_configure);
290327

291328
static u32 i2c_dw_acpi_round_bus_speed(struct device *device)
292329
{
@@ -308,11 +345,13 @@ static u32 i2c_dw_acpi_round_bus_speed(struct device *device)
308345

309346
#else /* CONFIG_ACPI */
310347

348+
static inline void i2c_dw_acpi_configure(struct device *device) { }
349+
311350
static inline u32 i2c_dw_acpi_round_bus_speed(struct device *device) { return 0; }
312351

313352
#endif /* CONFIG_ACPI */
314353

315-
void i2c_dw_adjust_bus_speed(struct dw_i2c_dev *dev)
354+
static void i2c_dw_adjust_bus_speed(struct dw_i2c_dev *dev)
316355
{
317356
u32 acpi_speed = i2c_dw_acpi_round_bus_speed(dev->dev);
318357
struct i2c_timings *t = &dev->timings;
@@ -328,7 +367,24 @@ void i2c_dw_adjust_bus_speed(struct dw_i2c_dev *dev)
328367
else
329368
t->bus_freq_hz = I2C_MAX_FAST_MODE_FREQ;
330369
}
331-
EXPORT_SYMBOL_GPL(i2c_dw_adjust_bus_speed);
370+
371+
int i2c_dw_fw_parse_and_configure(struct dw_i2c_dev *dev)
372+
{
373+
struct i2c_timings *t = &dev->timings;
374+
struct device *device = dev->dev;
375+
376+
i2c_parse_fw_timings(device, t, false);
377+
378+
i2c_dw_adjust_bus_speed(dev);
379+
380+
if (device->of_node)
381+
i2c_dw_of_configure(device);
382+
if (has_acpi_companion(device))
383+
i2c_dw_acpi_configure(device);
384+
385+
return i2c_dw_validate_speed(dev);
386+
}
387+
EXPORT_SYMBOL_GPL(i2c_dw_fw_parse_and_configure);
332388

333389
static u32 i2c_dw_read_scl_reg(struct dw_i2c_dev *dev, u32 reg)
334390
{

drivers/i2c/busses/i2c-designware-core.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,4 @@ int i2c_dw_baytrail_probe_lock_support(struct dw_i2c_dev *dev);
416416
int i2c_dw_amdpsp_probe_lock_support(struct dw_i2c_dev *dev);
417417
#endif
418418

419-
int i2c_dw_validate_speed(struct dw_i2c_dev *dev);
420-
void i2c_dw_adjust_bus_speed(struct dw_i2c_dev *dev);
421-
422-
#if IS_ENABLED(CONFIG_ACPI)
423-
void i2c_dw_acpi_configure(struct device *device);
424-
#else
425-
static inline void i2c_dw_acpi_configure(struct device *device) { }
426-
#endif
419+
int i2c_dw_fw_parse_and_configure(struct dw_i2c_dev *dev);

drivers/i2c/busses/i2c-designware-pcidrv.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ static int i2c_dw_pci_probe(struct pci_dev *pdev,
253253
int r;
254254
struct dw_pci_controller *controller;
255255
struct dw_scl_sda_cfg *cfg;
256-
struct i2c_timings *t;
257256

258257
if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers))
259258
return dev_err_probe(&pdev->dev, -EINVAL,
@@ -288,9 +287,6 @@ static int i2c_dw_pci_probe(struct pci_dev *pdev,
288287
dev->irq = pci_irq_vector(pdev, 0);
289288
dev->flags |= controller->flags;
290289

291-
t = &dev->timings;
292-
i2c_parse_fw_timings(&pdev->dev, t, false);
293-
294290
pci_set_drvdata(pdev, dev);
295291

296292
if (controller->setup) {
@@ -299,12 +295,7 @@ static int i2c_dw_pci_probe(struct pci_dev *pdev,
299295
return r;
300296
}
301297

302-
i2c_dw_adjust_bus_speed(dev);
303-
304-
if (has_acpi_companion(&pdev->dev))
305-
i2c_dw_acpi_configure(&pdev->dev);
306-
307-
r = i2c_dw_validate_speed(dev);
298+
r = i2c_dw_fw_parse_and_configure(dev);
308299
if (r)
309300
return r;
310301

drivers/i2c/busses/i2c-designware-platdrv.c

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <linux/kernel.h>
2222
#include <linux/mfd/syscon.h>
2323
#include <linux/module.h>
24-
#include <linux/of.h>
2524
#include <linux/platform_device.h>
2625
#include <linux/pm.h>
2726
#include <linux/pm_runtime.h>
@@ -97,43 +96,11 @@ static int bt1_i2c_request_regs(struct dw_i2c_dev *dev)
9796
dev->map = devm_regmap_init(dev->dev, NULL, dev, &bt1_i2c_cfg);
9897
return PTR_ERR_OR_ZERO(dev->map);
9998
}
100-
101-
#define MSCC_ICPU_CFG_TWI_DELAY 0x0
102-
#define MSCC_ICPU_CFG_TWI_DELAY_ENABLE BIT(0)
103-
#define MSCC_ICPU_CFG_TWI_SPIKE_FILTER 0x4
104-
105-
static int mscc_twi_set_sda_hold_time(struct dw_i2c_dev *dev)
106-
{
107-
writel((dev->sda_hold_time << 1) | MSCC_ICPU_CFG_TWI_DELAY_ENABLE,
108-
dev->ext + MSCC_ICPU_CFG_TWI_DELAY);
109-
110-
return 0;
111-
}
112-
113-
static void i2c_dw_of_configure(struct device *device)
114-
{
115-
struct platform_device *pdev = to_platform_device(device);
116-
struct dw_i2c_dev *dev = dev_get_drvdata(device);
117-
118-
switch (dev->flags & MODEL_MASK) {
119-
case MODEL_MSCC_OCELOT:
120-
dev->ext = devm_platform_ioremap_resource(pdev, 1);
121-
if (!IS_ERR(dev->ext))
122-
dev->set_sda_hold_time = mscc_twi_set_sda_hold_time;
123-
break;
124-
default:
125-
break;
126-
}
127-
}
12899
#else
129100
static int bt1_i2c_request_regs(struct dw_i2c_dev *dev)
130101
{
131102
return -ENODEV;
132103
}
133-
134-
static inline void i2c_dw_of_configure(struct device *device)
135-
{
136-
}
137104
#endif
138105

139106
static int txgbe_i2c_request_regs(struct dw_i2c_dev *dev)
@@ -242,7 +209,6 @@ static int dw_i2c_plat_probe(struct platform_device *pdev)
242209
{
243210
struct i2c_adapter *adap;
244211
struct dw_i2c_dev *dev;
245-
struct i2c_timings *t;
246212
int irq, ret;
247213

248214
irq = platform_get_irq(pdev, 0);
@@ -271,18 +237,7 @@ static int dw_i2c_plat_probe(struct platform_device *pdev)
271237

272238
reset_control_deassert(dev->rst);
273239

274-
t = &dev->timings;
275-
i2c_parse_fw_timings(&pdev->dev, t, false);
276-
277-
i2c_dw_adjust_bus_speed(dev);
278-
279-
if (pdev->dev.of_node)
280-
i2c_dw_of_configure(&pdev->dev);
281-
282-
if (has_acpi_companion(&pdev->dev))
283-
i2c_dw_acpi_configure(&pdev->dev);
284-
285-
ret = i2c_dw_validate_speed(dev);
240+
ret = i2c_dw_fw_parse_and_configure(dev);
286241
if (ret)
287242
goto exit_reset;
288243

@@ -310,6 +265,7 @@ static int dw_i2c_plat_probe(struct platform_device *pdev)
310265
goto exit_reset;
311266

312267
if (dev->clk) {
268+
struct i2c_timings *t = &dev->timings;
313269
u64 clk_khz;
314270

315271
dev->get_clk_rate_khz = i2c_dw_get_clk_rate_khz;

0 commit comments

Comments
 (0)