|
@@ -35,7 +35,8 @@ struct ocores_i2c {
|
|
int pos;
|
|
int pos;
|
|
int nmsgs;
|
|
int nmsgs;
|
|
int state; /* see STATE_ */
|
|
int state; /* see STATE_ */
|
|
- int clock_khz;
|
|
|
|
|
|
+ int ip_clock_khz;
|
|
|
|
+ int bus_clock_khz;
|
|
void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
|
|
void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value);
|
|
u8 (*getreg)(struct ocores_i2c *i2c, int reg);
|
|
u8 (*getreg)(struct ocores_i2c *i2c, int reg);
|
|
};
|
|
};
|
|
@@ -215,21 +216,34 @@ static int ocores_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
|
|
return -ETIMEDOUT;
|
|
return -ETIMEDOUT;
|
|
}
|
|
}
|
|
|
|
|
|
-static void ocores_init(struct ocores_i2c *i2c)
|
|
|
|
|
|
+static int ocores_init(struct device *dev, struct ocores_i2c *i2c)
|
|
{
|
|
{
|
|
int prescale;
|
|
int prescale;
|
|
|
|
+ int diff;
|
|
u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
|
|
u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL);
|
|
|
|
|
|
/* make sure the device is disabled */
|
|
/* make sure the device is disabled */
|
|
oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
|
|
oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~(OCI2C_CTRL_EN|OCI2C_CTRL_IEN));
|
|
|
|
|
|
- prescale = (i2c->clock_khz / (5*100)) - 1;
|
|
|
|
|
|
+ prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1;
|
|
|
|
+ prescale = clamp(prescale, 0, 0xffff);
|
|
|
|
+
|
|
|
|
+ diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz;
|
|
|
|
+ if (abs(diff) > i2c->bus_clock_khz / 10) {
|
|
|
|
+ dev_err(dev,
|
|
|
|
+ "Unsupported clock settings: core: %d KHz, bus: %d KHz\n",
|
|
|
|
+ i2c->ip_clock_khz, i2c->bus_clock_khz);
|
|
|
|
+ return -EINVAL;
|
|
|
|
+ }
|
|
|
|
+
|
|
oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
|
|
oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff);
|
|
oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
|
|
oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8);
|
|
|
|
|
|
/* Init the device */
|
|
/* Init the device */
|
|
oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
|
|
oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK);
|
|
oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN | OCI2C_CTRL_EN);
|
|
oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN | OCI2C_CTRL_EN);
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -304,6 +318,8 @@ static int ocores_i2c_of_probe(struct platform_device *pdev,
|
|
struct device_node *np = pdev->dev.of_node;
|
|
struct device_node *np = pdev->dev.of_node;
|
|
const struct of_device_id *match;
|
|
const struct of_device_id *match;
|
|
u32 val;
|
|
u32 val;
|
|
|
|
+ u32 clock_frequency;
|
|
|
|
+ bool clock_frequency_present;
|
|
|
|
|
|
if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) {
|
|
if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) {
|
|
/* no 'reg-shift', check for deprecated 'regstep' */
|
|
/* no 'reg-shift', check for deprecated 'regstep' */
|
|
@@ -319,12 +335,24 @@ static int ocores_i2c_of_probe(struct platform_device *pdev,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- if (of_property_read_u32(np, "clock-frequency", &val)) {
|
|
|
|
- dev_err(&pdev->dev,
|
|
|
|
- "Missing required parameter 'clock-frequency'\n");
|
|
|
|
- return -ENODEV;
|
|
|
|
|
|
+ clock_frequency_present = !of_property_read_u32(np, "clock-frequency",
|
|
|
|
+ &clock_frequency);
|
|
|
|
+ i2c->bus_clock_khz = 100;
|
|
|
|
+
|
|
|
|
+ if (of_property_read_u32(np, "opencores,ip-clock-frequency", &val)) {
|
|
|
|
+ if (!clock_frequency_present) {
|
|
|
|
+ dev_err(&pdev->dev,
|
|
|
|
+ "Missing required parameter 'opencores,ip-clock-frequency'\n");
|
|
|
|
+ return -ENODEV;
|
|
|
|
+ }
|
|
|
|
+ i2c->ip_clock_khz = clock_frequency / 1000;
|
|
|
|
+ dev_warn(&pdev->dev,
|
|
|
|
+ "Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequency'\n");
|
|
|
|
+ } else {
|
|
|
|
+ i2c->ip_clock_khz = val / 1000;
|
|
|
|
+ if (clock_frequency_present)
|
|
|
|
+ i2c->bus_clock_khz = clock_frequency / 1000;
|
|
}
|
|
}
|
|
- i2c->clock_khz = val / 1000;
|
|
|
|
|
|
|
|
of_property_read_u32(pdev->dev.of_node, "reg-io-width",
|
|
of_property_read_u32(pdev->dev.of_node, "reg-io-width",
|
|
&i2c->reg_io_width);
|
|
&i2c->reg_io_width);
|
|
@@ -368,7 +396,8 @@ static int ocores_i2c_probe(struct platform_device *pdev)
|
|
if (pdata) {
|
|
if (pdata) {
|
|
i2c->reg_shift = pdata->reg_shift;
|
|
i2c->reg_shift = pdata->reg_shift;
|
|
i2c->reg_io_width = pdata->reg_io_width;
|
|
i2c->reg_io_width = pdata->reg_io_width;
|
|
- i2c->clock_khz = pdata->clock_khz;
|
|
|
|
|
|
+ i2c->ip_clock_khz = pdata->clock_khz;
|
|
|
|
+ i2c->bus_clock_khz = 100;
|
|
} else {
|
|
} else {
|
|
ret = ocores_i2c_of_probe(pdev, i2c);
|
|
ret = ocores_i2c_of_probe(pdev, i2c);
|
|
if (ret)
|
|
if (ret)
|
|
@@ -402,7 +431,9 @@ static int ocores_i2c_probe(struct platform_device *pdev)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- ocores_init(i2c);
|
|
|
|
|
|
+ ret = ocores_init(&pdev->dev, i2c);
|
|
|
|
+ if (ret)
|
|
|
|
+ return ret;
|
|
|
|
|
|
init_waitqueue_head(&i2c->wait);
|
|
init_waitqueue_head(&i2c->wait);
|
|
ret = devm_request_irq(&pdev->dev, irq, ocores_isr, 0,
|
|
ret = devm_request_irq(&pdev->dev, irq, ocores_isr, 0,
|
|
@@ -465,9 +496,7 @@ static int ocores_i2c_resume(struct device *dev)
|
|
{
|
|
{
|
|
struct ocores_i2c *i2c = dev_get_drvdata(dev);
|
|
struct ocores_i2c *i2c = dev_get_drvdata(dev);
|
|
|
|
|
|
- ocores_init(i2c);
|
|
|
|
-
|
|
|
|
- return 0;
|
|
|
|
|
|
+ return ocores_init(dev, i2c);
|
|
}
|
|
}
|
|
|
|
|
|
static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume);
|
|
static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume);
|