8f578ab71eeccd7a0e52b4c776ebbad323d2b6df
[openwrt.git] / target / linux / brcm2708 / patches-4.4 / 0122-Extend-clock-timeout-fix-modprobe-baudrate-parameter.patch
1 From ba2e42fe23631e6cf1f2e1167618ab1c13d9b3ba Mon Sep 17 00:00:00 2001
2 From: Devon Fyson <devonfyson@gmail.com>
3 Date: Wed, 30 Dec 2015 16:40:47 -0500
4 Subject: [PATCH 122/127] Extend clock timeout, fix modprobe baudrate
5  parameter.
6
7 Set the BSC_CLKT clock streching timeout to 35ms as per SMBus specs.\n- Increase priority of baudrate parameter passed to modprobe (in /etc/modprobe.d/*.conf or command line). Currently custom baudrates don't work because they are overridden by clock-frequency in the platform_device passed to the function.
8 ---
9  drivers/i2c/busses/i2c-bcm2708.c | 45 ++++++++++++++++++++++++++--------------
10  1 file changed, 29 insertions(+), 16 deletions(-)
11
12 --- a/drivers/i2c/busses/i2c-bcm2708.c
13 +++ b/drivers/i2c/busses/i2c-bcm2708.c
14 @@ -71,7 +71,8 @@
15  
16  #define DRV_NAME               "bcm2708_i2c"
17  
18 -static unsigned int baudrate = CONFIG_I2C_BCM2708_BAUDRATE;
19 +static unsigned int baudrate_default = CONFIG_I2C_BCM2708_BAUDRATE;
20 +static unsigned int baudrate;
21  module_param(baudrate, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
22  MODULE_PARM_DESC(baudrate, "The I2C baudrate");
23  
24 @@ -87,6 +88,7 @@ struct bcm2708_i2c {
25         int irq;
26         struct clk *clk;
27         u32 cdiv;
28 +       u32 clk_tout;
29  
30         struct completion done;
31  
32 @@ -126,7 +128,7 @@ static inline void bcm2708_bsc_fifo_fill
33  
34  static inline int bcm2708_bsc_setup(struct bcm2708_i2c *bi)
35  {
36 -       u32 cdiv, s;
37 +       u32 cdiv, s, clk_tout;
38         u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
39         int wait_loops = I2C_WAIT_LOOP_COUNT;
40  
41 @@ -134,12 +136,14 @@ static inline int bcm2708_bsc_setup(stru
42          * Use the value that we cached in the probe.
43          */
44         cdiv = bi->cdiv;
45 +       clk_tout = bi->clk_tout;
46  
47         if (bi->msg->flags & I2C_M_RD)
48                 c |= BSC_C_INTR | BSC_C_READ;
49         else
50                 c |= BSC_C_INTT;
51  
52 +       bcm2708_wr(bi, BSC_CLKT, clk_tout);
53         bcm2708_wr(bi, BSC_DIV, cdiv);
54         bcm2708_wr(bi, BSC_A, bi->msg->addr);
55         bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
56 @@ -312,21 +316,24 @@ static int bcm2708_i2c_probe(struct plat
57         struct bcm2708_i2c *bi;
58         struct i2c_adapter *adap;
59         unsigned long bus_hz;
60 -       u32 cdiv;
61 -
62 -       if (pdev->dev.of_node) {
63 -               u32 bus_clk_rate;
64 -               pdev->id = of_alias_get_id(pdev->dev.of_node, "i2c");
65 -               if (pdev->id < 0) {
66 -                       dev_err(&pdev->dev, "alias is missing\n");
67 -                       return -EINVAL;
68 +       u32 cdiv, clk_tout;
69 +       
70 +       if (!baudrate) {
71 +               baudrate = baudrate_default;
72 +               if (pdev->dev.of_node) {
73 +                       u32 bus_clk_rate;
74 +                       pdev->id = of_alias_get_id(pdev->dev.of_node, "i2c");
75 +                       if (pdev->id < 0) {
76 +                               dev_err(&pdev->dev, "alias is missing\n");
77 +                               return -EINVAL;
78 +                       }
79 +                       if (!of_property_read_u32(pdev->dev.of_node,
80 +                                               "clock-frequency", &bus_clk_rate))
81 +                               baudrate = bus_clk_rate;
82 +                       else
83 +                               dev_warn(&pdev->dev,
84 +                                       "Could not read clock-frequency property\n");
85                 }
86 -               if (!of_property_read_u32(pdev->dev.of_node,
87 -                                       "clock-frequency", &bus_clk_rate))
88 -                       baudrate = bus_clk_rate;
89 -               else
90 -                       dev_warn(&pdev->dev,
91 -                               "Could not read clock-frequency property\n");
92         }
93  
94         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
95 @@ -417,7 +424,13 @@ static int bcm2708_i2c_probe(struct plat
96                 cdiv = 0xffff;
97                 baudrate = bus_hz / cdiv;
98         }
99 +       
100 +       clk_tout = 35/1000*baudrate; //35ms timeout as per SMBus specs.
101 +       if (clk_tout > 0xffff)
102 +               clk_tout = 0xffff;
103 +       
104         bi->cdiv = cdiv;
105 +       bi->clk_tout = clk_tout;
106  
107         dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %d)\n",
108                 pdev->id, (unsigned long)regs->start, irq, baudrate);