blob: 58213d9714cd47715e7d58a818ecf976c47e226e [file] [log] [blame]
Jarkko Nikula85d05fb2007-11-07 06:54:31 +02001/*
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 *
Jarkko Nikuladdf25df2009-05-25 11:08:43 -07008 * Contact: Jarkko Nikula <jhnikula@gmail.com>
Jarkko Nikula85d05fb2007-11-07 06:54:31 +02009 *
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>
Tony Lindgren3a8761c2012-10-08 09:11:22 -070029#include <linux/i2c-omap.h>
Paul Walmsley4d17aeb2010-09-21 19:37:15 +053030#include <linux/slab.h>
31#include <linux/err.h>
32#include <linux/clk.h>
Kalle Jokiniemi20c9d2c2010-05-11 11:35:08 -070033
Tony Lindgren01480bad2012-10-30 10:33:24 -070034#include <plat/i2c.h>
Tony Lindgrene4c060d2012-10-05 13:25:59 -070035
Paul Walmsley4d17aeb2010-09-21 19:37:15 +053036#define OMAP_I2C_MAX_CONTROLLERS 4
37static struct omap_i2c_bus_platform_data i2c_pdata[OMAP_I2C_MAX_CONTROLLERS];
Jarkko Nikula85d05fb2007-11-07 06:54:31 +020038
Jarkko Nikula79547632009-03-23 18:07:48 -070039#define OMAP_I2C_CMDLINE_SETUP (BIT(31))
40
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070041/**
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{
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070054 int ints[3];
55
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070056 get_options(str, 3, ints);
Tony Lindgrenc34f7c62012-10-29 16:17:59 -070057 if (ints[0] < 2 || ints[1] < 1 ||
58 ints[1] > OMAP_I2C_MAX_CONTROLLERS)
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070059 return 0;
Kalle Jokiniemi20c9d2c2010-05-11 11:35:08 -070060 i2c_pdata[ints[1] - 1].clkrate = ints[2];
61 i2c_pdata[ints[1] - 1].clkrate |= OMAP_I2C_CMDLINE_SETUP;
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070062
63 return 1;
64}
65__setup("i2c_bus=", omap_i2c_bus_setup);
66
Jarkko Nikula79547632009-03-23 18:07:48 -070067/*
68 * Register busses defined in command line but that are not registered with
69 * omap_register_i2c_bus from board initialization code.
70 */
Tony Lindgrena6cf9122013-01-11 11:24:19 -080071int __init omap_register_i2c_bus_cmdline(void)
Jarkko Nikula79547632009-03-23 18:07:48 -070072{
73 int i, err = 0;
74
Kalle Jokiniemi20c9d2c2010-05-11 11:35:08 -070075 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;
Tony Lindgren3a8761c2012-10-08 09:11:22 -070078 err = omap_i2c_add_bus(&i2c_pdata[i], i + 1);
Jarkko Nikula79547632009-03-23 18:07:48 -070079 if (err)
80 goto out;
81 }
82
83out:
84 return err;
85}
Jarkko Nikula79547632009-03-23 18:07:48 -070086
Jarkko Nikulad4c58bf2009-03-23 18:07:46 -070087/**
Jarkko Nikula9833eff2010-02-22 20:29:36 +000088 * omap_register_i2c_bus - register I2C bus with device descriptors
Jarkko Nikulad4c58bf2009-03-23 18:07:46 -070089 * @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 */
Jarkko Nikula9833eff2010-02-22 20:29:36 +000096int __init omap_register_i2c_bus(int bus_id, u32 clkrate,
Jarkko Nikula85d05fb2007-11-07 06:54:31 +020097 struct i2c_board_info const *info,
98 unsigned len)
99{
Jarkko Nikula3a853fb2009-03-23 18:07:47 -0700100 int err;
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200101
Tony Lindgrenc34f7c62012-10-29 16:17:59 -0700102 BUG_ON(bus_id < 1 || bus_id > OMAP_I2C_MAX_CONTROLLERS);
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200103
104 if (info) {
105 err = i2c_register_board_info(bus_id, info, len);
106 if (err)
107 return err;
108 }
109
Kalle Jokiniemi20c9d2c2010-05-11 11:35:08 -0700110 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;
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200114
Tony Lindgren3a8761c2012-10-08 09:11:22 -0700115 return omap_i2c_add_bus(&i2c_pdata[bus_id - 1], bus_id);
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200116}