Merge tag 'v4.2-rc8' into x86/mm, before applying new changes
[linux-2.6-block.git] / arch / arm / plat-omap / i2c.c
CommitLineData
85d05fb3
JN
1/*
2 * linux/arch/arm/plat-omap/i2c.c
3 *
4 * Helper module for board specific I2C bus registration
5 *
6 * Copyright (C) 2007 Nokia Corporation.
7 *
ddf25dfe 8 * Contact: Jarkko Nikula <jhnikula@gmail.com>
85d05fb3
JN
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * version 2 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 *
24 */
25
26#include <linux/kernel.h>
27#include <linux/platform_device.h>
28#include <linux/i2c.h>
3a8761c0 29#include <linux/i2c-omap.h>
4d17aeb1
PW
30#include <linux/slab.h>
31#include <linux/err.h>
32#include <linux/clk.h>
20c9d2c4 33
01480bad 34#include <plat/i2c.h>
e4c060db 35
4d17aeb1
PW
36#define OMAP_I2C_MAX_CONTROLLERS 4
37static struct omap_i2c_bus_platform_data i2c_pdata[OMAP_I2C_MAX_CONTROLLERS];
85d05fb3 38
7954763b
JN
39#define OMAP_I2C_CMDLINE_SETUP (BIT(31))
40
3a853fb9
JN
41/**
42 * omap_i2c_bus_setup - Process command line options for the I2C bus speed
43 * @str: String of options
44 *
45 * This function allow to override the default I2C bus speed for given I2C
46 * bus with a command line option.
47 *
48 * Format: i2c_bus=bus_id,clkrate (in kHz)
49 *
50 * Returns 1 on success, 0 otherwise.
51 */
52static int __init omap_i2c_bus_setup(char *str)
53{
3a853fb9
JN
54 int ints[3];
55
3a853fb9 56 get_options(str, 3, ints);
c34f7c69
TL
57 if (ints[0] < 2 || ints[1] < 1 ||
58 ints[1] > OMAP_I2C_MAX_CONTROLLERS)
3a853fb9 59 return 0;
20c9d2c4
KJ
60 i2c_pdata[ints[1] - 1].clkrate = ints[2];
61 i2c_pdata[ints[1] - 1].clkrate |= OMAP_I2C_CMDLINE_SETUP;
3a853fb9
JN
62
63 return 1;
64}
65__setup("i2c_bus=", omap_i2c_bus_setup);
66
7954763b
JN
67/*
68 * Register busses defined in command line but that are not registered with
69 * omap_register_i2c_bus from board initialization code.
70 */
a6cf912c 71int __init omap_register_i2c_bus_cmdline(void)
7954763b
JN
72{
73 int i, err = 0;
74
20c9d2c4
KJ
75 for (i = 0; i < ARRAY_SIZE(i2c_pdata); i++)
76 if (i2c_pdata[i].clkrate & OMAP_I2C_CMDLINE_SETUP) {
77 i2c_pdata[i].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
3a8761c0 78 err = omap_i2c_add_bus(&i2c_pdata[i], i + 1);
7954763b
JN
79 if (err)
80 goto out;
81 }
82
83out:
84 return err;
85}
7954763b 86
d4c58bf4 87/**
9833eff3 88 * omap_register_i2c_bus - register I2C bus with device descriptors
d4c58bf4
JN
89 * @bus_id: bus id counting from number 1
90 * @clkrate: clock rate of the bus in kHz
91 * @info: pointer into I2C device descriptor table or NULL
92 * @len: number of descriptors in the table
93 *
94 * Returns 0 on success or an error code.
95 */
9833eff3 96int __init omap_register_i2c_bus(int bus_id, u32 clkrate,
85d05fb3
JN
97 struct i2c_board_info const *info,
98 unsigned len)
99{
3a853fb9 100 int err;
85d05fb3 101
c34f7c69 102 BUG_ON(bus_id < 1 || bus_id > OMAP_I2C_MAX_CONTROLLERS);
85d05fb3
JN
103
104 if (info) {
105 err = i2c_register_board_info(bus_id, info, len);
106 if (err)
107 return err;
108 }
109
20c9d2c4
KJ
110 if (!i2c_pdata[bus_id - 1].clkrate)
111 i2c_pdata[bus_id - 1].clkrate = clkrate;
112
113 i2c_pdata[bus_id - 1].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
85d05fb3 114
3a8761c0 115 return omap_i2c_add_bus(&i2c_pdata[bus_id - 1], bus_id);
85d05fb3 116}