blob: 32f6c53367bf3c3dd0cdd4ab86f2436f5399f2d3 [file] [log] [blame]
Tony Lindgrenb63128e2009-12-11 16:16:32 -08001/*
2 * Helper module for board specific I2C bus registration
3 *
4 * Copyright (C) 2009 Nokia Corporation.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 *
20 */
21
Tony Lindgren65fa3e72016-10-17 00:06:54 -070022#include <linux/i2c.h>
Tony Lindgren3a8761c2012-10-08 09:11:22 -070023#include <linux/i2c-omap.h>
Tony Lindgren70c494c2012-09-19 10:46:56 -070024#include <mach/mux.h>
Tony Lindgrene4c060d2012-10-05 13:25:59 -070025#include "soc.h"
Tony Lindgrenb63128e2009-12-11 16:16:32 -080026
Tony Lindgren01480bad2012-10-30 10:33:24 -070027#include <plat/i2c.h>
Tony Lindgren3a8761c2012-10-08 09:11:22 -070028
29#define OMAP_I2C_SIZE 0x3f
30#define OMAP1_I2C_BASE 0xfffb3800
Tony Lindgren3a8761c2012-10-08 09:11:22 -070031
32static const char name[] = "omap_i2c";
33
34static struct resource i2c_resources[2] = {
35};
36
37static struct platform_device omap_i2c_devices[1] = {
38};
39
40static void __init omap1_i2c_mux_pins(int bus_id)
Tony Lindgrenb63128e2009-12-11 16:16:32 -080041{
Cory Maccarronebf92a402009-12-11 16:16:34 -080042 if (cpu_is_omap7xx()) {
43 omap_cfg_reg(I2C_7XX_SDA);
44 omap_cfg_reg(I2C_7XX_SCL);
45 } else {
46 omap_cfg_reg(I2C_SDA);
47 omap_cfg_reg(I2C_SCL);
48 }
Tony Lindgrenb63128e2009-12-11 16:16:32 -080049}
Tony Lindgren3a8761c2012-10-08 09:11:22 -070050
51int __init omap_i2c_add_bus(struct omap_i2c_bus_platform_data *pdata,
52 int bus_id)
53{
54 struct platform_device *pdev;
55 struct resource *res;
56
Tony Lindgrenc34f7c62012-10-29 16:17:59 -070057 if (bus_id > 1)
58 return -EINVAL;
59
Tony Lindgren3a8761c2012-10-08 09:11:22 -070060 omap1_i2c_mux_pins(bus_id);
61
62 pdev = &omap_i2c_devices[bus_id - 1];
63 pdev->id = bus_id;
64 pdev->name = name;
65 pdev->num_resources = ARRAY_SIZE(i2c_resources);
66 res = i2c_resources;
67 res[0].start = OMAP1_I2C_BASE;
68 res[0].end = res[0].start + OMAP_I2C_SIZE;
69 res[0].flags = IORESOURCE_MEM;
Tony Lindgren685e2d02015-05-20 09:01:21 -070070 res[1].start = INT_I2C;
Tony Lindgren3a8761c2012-10-08 09:11:22 -070071 res[1].flags = IORESOURCE_IRQ;
72 pdev->resource = res;
73
74 /* all OMAP1 have IP version 1 register set */
75 pdata->rev = OMAP_I2C_IP_VERSION_1;
76
77 /* all OMAP1 I2C are implemented like this */
78 pdata->flags = OMAP_I2C_FLAG_NO_FIFO |
79 OMAP_I2C_FLAG_SIMPLE_CLOCK |
80 OMAP_I2C_FLAG_16BIT_DATA_REG |
81 OMAP_I2C_FLAG_ALWAYS_ARMXOR_CLK;
82
83 /* how the cpu bus is wired up differs for 7xx only */
84
85 if (cpu_is_omap7xx())
86 pdata->flags |= OMAP_I2C_FLAG_BUS_SHIFT_1;
87 else
88 pdata->flags |= OMAP_I2C_FLAG_BUS_SHIFT_2;
89
90 pdev->dev.platform_data = pdata;
91
92 return platform_device_register(pdev);
93}
Tony Lindgrena6cf9122013-01-11 11:24:19 -080094
Tony Lindgren65fa3e72016-10-17 00:06:54 -070095#define OMAP_I2C_MAX_CONTROLLERS 4
96static struct omap_i2c_bus_platform_data i2c_pdata[OMAP_I2C_MAX_CONTROLLERS];
97
98#define OMAP_I2C_CMDLINE_SETUP (BIT(31))
99
100/**
101 * omap_i2c_bus_setup - Process command line options for the I2C bus speed
102 * @str: String of options
103 *
104 * This function allow to override the default I2C bus speed for given I2C
105 * bus with a command line option.
106 *
107 * Format: i2c_bus=bus_id,clkrate (in kHz)
108 *
109 * Returns 1 on success, 0 otherwise.
110 */
111static int __init omap_i2c_bus_setup(char *str)
112{
113 int ints[3];
114
115 get_options(str, 3, ints);
116 if (ints[0] < 2 || ints[1] < 1 ||
117 ints[1] > OMAP_I2C_MAX_CONTROLLERS)
118 return 0;
119 i2c_pdata[ints[1] - 1].clkrate = ints[2];
120 i2c_pdata[ints[1] - 1].clkrate |= OMAP_I2C_CMDLINE_SETUP;
121
122 return 1;
123}
124__setup("i2c_bus=", omap_i2c_bus_setup);
125
126/*
127 * Register busses defined in command line but that are not registered with
128 * omap_register_i2c_bus from board initialization code.
129 */
130int __init omap_register_i2c_bus_cmdline(void)
131{
132 int i, err = 0;
133
134 for (i = 0; i < ARRAY_SIZE(i2c_pdata); i++)
135 if (i2c_pdata[i].clkrate & OMAP_I2C_CMDLINE_SETUP) {
136 i2c_pdata[i].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
137 err = omap_i2c_add_bus(&i2c_pdata[i], i + 1);
138 if (err)
139 goto out;
140 }
141
142out:
143 return err;
144}
145
146/**
147 * omap_register_i2c_bus - register I2C bus with device descriptors
148 * @bus_id: bus id counting from number 1
149 * @clkrate: clock rate of the bus in kHz
150 * @info: pointer into I2C device descriptor table or NULL
151 * @len: number of descriptors in the table
152 *
153 * Returns 0 on success or an error code.
154 */
155int __init omap_register_i2c_bus(int bus_id, u32 clkrate,
156 struct i2c_board_info const *info,
157 unsigned len)
158{
159 int err;
160
161 BUG_ON(bus_id < 1 || bus_id > OMAP_I2C_MAX_CONTROLLERS);
162
163 if (info) {
164 err = i2c_register_board_info(bus_id, info, len);
165 if (err)
166 return err;
167 }
168
169 if (!i2c_pdata[bus_id - 1].clkrate)
170 i2c_pdata[bus_id - 1].clkrate = clkrate;
171
172 i2c_pdata[bus_id - 1].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
173
174 return omap_i2c_add_bus(&i2c_pdata[bus_id - 1], bus_id);
175}
176
Tony Lindgrena6cf9122013-01-11 11:24:19 -0800177static int __init omap_i2c_cmdline(void)
178{
179 return omap_register_i2c_bus_cmdline();
180}
181subsys_initcall(omap_i2c_cmdline);