diff options
author | Wolfram Sang <wsa+renesas@sang-engineering.com> | 2015-12-08 04:37:46 -0500 |
---|---|---|
committer | Wolfram Sang <wsa@the-dreams.de> | 2015-12-14 04:51:07 -0500 |
commit | e1dba01ca620bb0b3864d5237c1c597d9e012ebf (patch) | |
tree | 44b4a1e51fbb14fc803f2cbd18a13c0509eda207 | |
parent | 99b809d7bdd17de7a68f1625a8973052c8311657 (diff) |
i2c: add generic routine to parse DT for timing information
Inspired from the i2c-rk3x driver (thanks guys!) but refactored and
extended. See built-in docs for further information.
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
-rw-r--r-- | drivers/i2c/i2c-core.c | 53 | ||||
-rw-r--r-- | include/linux/i2c.h | 18 |
2 files changed, 71 insertions, 0 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index ba8eb087f224..b34c412bd2c2 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c | |||
@@ -53,6 +53,7 @@ | |||
53 | #include <linux/jump_label.h> | 53 | #include <linux/jump_label.h> |
54 | #include <asm/uaccess.h> | 54 | #include <asm/uaccess.h> |
55 | #include <linux/err.h> | 55 | #include <linux/err.h> |
56 | #include <linux/property.h> | ||
56 | 57 | ||
57 | #include "i2c-core.h" | 58 | #include "i2c-core.h" |
58 | 59 | ||
@@ -1438,6 +1439,58 @@ static void of_i2c_register_devices(struct i2c_adapter *adap) | |||
1438 | } | 1439 | } |
1439 | } | 1440 | } |
1440 | 1441 | ||
1442 | /** | ||
1443 | * i2c_parse_fw_timings - get I2C related timing parameters from firmware | ||
1444 | * @dev: The device to scan for I2C timing properties | ||
1445 | * @t: the i2c_timings struct to be filled with values | ||
1446 | * @use_defaults: bool to use sane defaults derived from the I2C specification | ||
1447 | * when properties are not found, otherwise use 0 | ||
1448 | * | ||
1449 | * Scan the device for the generic I2C properties describing timing parameters | ||
1450 | * for the signal and fill the given struct with the results. If a property was | ||
1451 | * not found and use_defaults was true, then maximum timings are assumed which | ||
1452 | * are derived from the I2C specification. If use_defaults is not used, the | ||
1453 | * results will be 0, so drivers can apply their own defaults later. The latter | ||
1454 | * is mainly intended for avoiding regressions of existing drivers which want | ||
1455 | * to switch to this function. New drivers almost always should use the defaults. | ||
1456 | */ | ||
1457 | |||
1458 | void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults) | ||
1459 | { | ||
1460 | int ret; | ||
1461 | |||
1462 | memset(t, 0, sizeof(*t)); | ||
1463 | |||
1464 | ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz); | ||
1465 | if (ret && use_defaults) | ||
1466 | t->bus_freq_hz = 100000; | ||
1467 | |||
1468 | ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns); | ||
1469 | if (ret && use_defaults) { | ||
1470 | if (t->bus_freq_hz <= 100000) | ||
1471 | t->scl_rise_ns = 1000; | ||
1472 | else if (t->bus_freq_hz <= 400000) | ||
1473 | t->scl_rise_ns = 300; | ||
1474 | else | ||
1475 | t->scl_rise_ns = 120; | ||
1476 | } | ||
1477 | |||
1478 | ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns); | ||
1479 | if (ret && use_defaults) { | ||
1480 | if (t->bus_freq_hz <= 400000) | ||
1481 | t->scl_fall_ns = 300; | ||
1482 | else | ||
1483 | t->scl_fall_ns = 120; | ||
1484 | } | ||
1485 | |||
1486 | device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns); | ||
1487 | |||
1488 | ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns); | ||
1489 | if (ret && use_defaults) | ||
1490 | t->sda_fall_ns = t->scl_fall_ns; | ||
1491 | } | ||
1492 | EXPORT_SYMBOL_GPL(i2c_parse_fw_timings); | ||
1493 | |||
1441 | static int of_dev_node_match(struct device *dev, void *data) | 1494 | static int of_dev_node_match(struct device *dev, void *data) |
1442 | { | 1495 | { |
1443 | return dev->of_node == data; | 1496 | return dev->of_node == data; |
diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 51028f351d13..bc2b19ad9357 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h | |||
@@ -414,6 +414,22 @@ struct i2c_algorithm { | |||
414 | }; | 414 | }; |
415 | 415 | ||
416 | /** | 416 | /** |
417 | * struct i2c_timings - I2C timing information | ||
418 | * @bus_freq_hz: the bus frequency in Hz | ||
419 | * @scl_rise_ns: time SCL signal takes to rise in ns; t(r) in the I2C specification | ||
420 | * @scl_fall_ns: time SCL signal takes to fall in ns; t(f) in the I2C specification | ||
421 | * @scl_int_delay_ns: time IP core additionally needs to setup SCL in ns | ||
422 | * @sda_fall_ns: time SDA signal takes to fall in ns; t(f) in the I2C specification | ||
423 | */ | ||
424 | struct i2c_timings { | ||
425 | u32 bus_freq_hz; | ||
426 | u32 scl_rise_ns; | ||
427 | u32 scl_fall_ns; | ||
428 | u32 scl_int_delay_ns; | ||
429 | u32 sda_fall_ns; | ||
430 | }; | ||
431 | |||
432 | /** | ||
417 | * struct i2c_bus_recovery_info - I2C bus recovery information | 433 | * struct i2c_bus_recovery_info - I2C bus recovery information |
418 | * @recover_bus: Recover routine. Either pass driver's recover_bus() routine, or | 434 | * @recover_bus: Recover routine. Either pass driver's recover_bus() routine, or |
419 | * i2c_generic_scl_recovery() or i2c_generic_gpio_recovery(). | 435 | * i2c_generic_scl_recovery() or i2c_generic_gpio_recovery(). |
@@ -604,6 +620,7 @@ extern void i2c_clients_command(struct i2c_adapter *adap, | |||
604 | extern struct i2c_adapter *i2c_get_adapter(int nr); | 620 | extern struct i2c_adapter *i2c_get_adapter(int nr); |
605 | extern void i2c_put_adapter(struct i2c_adapter *adap); | 621 | extern void i2c_put_adapter(struct i2c_adapter *adap); |
606 | 622 | ||
623 | void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults); | ||
607 | 624 | ||
608 | /* Return the functionality mask */ | 625 | /* Return the functionality mask */ |
609 | static inline u32 i2c_get_functionality(struct i2c_adapter *adap) | 626 | static inline u32 i2c_get_functionality(struct i2c_adapter *adap) |
@@ -660,6 +677,7 @@ extern struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node) | |||
660 | 677 | ||
661 | /* must call i2c_put_adapter() when done with returned i2c_adapter device */ | 678 | /* must call i2c_put_adapter() when done with returned i2c_adapter device */ |
662 | struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node); | 679 | struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node); |
680 | |||
663 | #else | 681 | #else |
664 | 682 | ||
665 | static inline struct i2c_client *of_find_i2c_device_by_node(struct device_node *node) | 683 | static inline struct i2c_client *of_find_i2c_device_by_node(struct device_node *node) |