blob: 4645dd40b886c84c8a82d6b4babd2cb08818984e [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
Tony Lindgren01480bad2012-10-30 10:33:24 -070036#include <mach/irqs.h>
Jarkko Nikula85d05fb2007-11-07 06:54:31 +020037
Paul Walmsley4d17aeb2010-09-21 19:37:15 +053038#define OMAP_I2C_MAX_CONTROLLERS 4
39static struct omap_i2c_bus_platform_data i2c_pdata[OMAP_I2C_MAX_CONTROLLERS];
Jarkko Nikula85d05fb2007-11-07 06:54:31 +020040
Jarkko Nikula79547632009-03-23 18:07:48 -070041#define OMAP_I2C_CMDLINE_SETUP (BIT(31))
42
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070043/**
44 * omap_i2c_bus_setup - Process command line options for the I2C bus speed
45 * @str: String of options
46 *
47 * This function allow to override the default I2C bus speed for given I2C
48 * bus with a command line option.
49 *
50 * Format: i2c_bus=bus_id,clkrate (in kHz)
51 *
52 * Returns 1 on success, 0 otherwise.
53 */
54static int __init omap_i2c_bus_setup(char *str)
55{
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070056 int ints[3];
57
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070058 get_options(str, 3, ints);
Tony Lindgrenc34f7c62012-10-29 16:17:59 -070059 if (ints[0] < 2 || ints[1] < 1 ||
60 ints[1] > OMAP_I2C_MAX_CONTROLLERS)
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070061 return 0;
Kalle Jokiniemi20c9d2c2010-05-11 11:35:08 -070062 i2c_pdata[ints[1] - 1].clkrate = ints[2];
63 i2c_pdata[ints[1] - 1].clkrate |= OMAP_I2C_CMDLINE_SETUP;
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070064
65 return 1;
66}
67__setup("i2c_bus=", omap_i2c_bus_setup);
68
Jarkko Nikula79547632009-03-23 18:07:48 -070069/*
70 * Register busses defined in command line but that are not registered with
71 * omap_register_i2c_bus from board initialization code.
72 */
73static int __init omap_register_i2c_bus_cmdline(void)
74{
75 int i, err = 0;
76
Kalle Jokiniemi20c9d2c2010-05-11 11:35:08 -070077 for (i = 0; i < ARRAY_SIZE(i2c_pdata); i++)
78 if (i2c_pdata[i].clkrate & OMAP_I2C_CMDLINE_SETUP) {
79 i2c_pdata[i].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
Tony Lindgren3a8761c2012-10-08 09:11:22 -070080 err = omap_i2c_add_bus(&i2c_pdata[i], i + 1);
Jarkko Nikula79547632009-03-23 18:07:48 -070081 if (err)
82 goto out;
83 }
84
85out:
86 return err;
87}
88subsys_initcall(omap_register_i2c_bus_cmdline);
89
Jarkko Nikulad4c58bf2009-03-23 18:07:46 -070090/**
Jarkko Nikula9833eff2010-02-22 20:29:36 +000091 * omap_register_i2c_bus - register I2C bus with device descriptors
Jarkko Nikulad4c58bf2009-03-23 18:07:46 -070092 * @bus_id: bus id counting from number 1
93 * @clkrate: clock rate of the bus in kHz
94 * @info: pointer into I2C device descriptor table or NULL
95 * @len: number of descriptors in the table
96 *
97 * Returns 0 on success or an error code.
98 */
Jarkko Nikula9833eff2010-02-22 20:29:36 +000099int __init omap_register_i2c_bus(int bus_id, u32 clkrate,
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200100 struct i2c_board_info const *info,
101 unsigned len)
102{
Jarkko Nikula3a853fb2009-03-23 18:07:47 -0700103 int err;
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200104
Tony Lindgrenc34f7c62012-10-29 16:17:59 -0700105 BUG_ON(bus_id < 1 || bus_id > OMAP_I2C_MAX_CONTROLLERS);
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200106
107 if (info) {
108 err = i2c_register_board_info(bus_id, info, len);
109 if (err)
110 return err;
111 }
112
Kalle Jokiniemi20c9d2c2010-05-11 11:35:08 -0700113 if (!i2c_pdata[bus_id - 1].clkrate)
114 i2c_pdata[bus_id - 1].clkrate = clkrate;
115
116 i2c_pdata[bus_id - 1].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200117
Tony Lindgren3a8761c2012-10-08 09:11:22 -0700118 return omap_i2c_add_bus(&i2c_pdata[bus_id - 1], bus_id);
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200119}