blob: be6deb7c12ec8fe9df3bef6315814b6e1206ed68 [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
Russell King80b02c12009-01-08 10:01:47 +000034#include <mach/irqs.h>
Tony Lindgrene4c060d2012-10-05 13:25:59 -070035
36#include "../mach-omap1/soc.h"
37#include "../mach-omap2/soc.h"
Jarkko Nikula85d05fb2007-11-07 06:54:31 +020038
Tony Lindgren3a8761c2012-10-08 09:11:22 -070039#include "i2c.h"
Jarkko Nikula85d05fb2007-11-07 06:54:31 +020040
Paul Walmsley4d17aeb2010-09-21 19:37:15 +053041#define OMAP_I2C_MAX_CONTROLLERS 4
42static struct omap_i2c_bus_platform_data i2c_pdata[OMAP_I2C_MAX_CONTROLLERS];
Jarkko Nikula85d05fb2007-11-07 06:54:31 +020043
Jarkko Nikula79547632009-03-23 18:07:48 -070044#define OMAP_I2C_CMDLINE_SETUP (BIT(31))
45
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070046static int __init omap_i2c_nr_ports(void)
47{
48 int ports = 0;
49
50 if (cpu_class_is_omap1())
51 ports = 1;
52 else if (cpu_is_omap24xx())
53 ports = 2;
54 else if (cpu_is_omap34xx())
55 ports = 3;
Tony Lindgren6daa6422010-05-20 11:36:43 -070056 else if (cpu_is_omap44xx())
57 ports = 4;
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070058
59 return ports;
60}
61
62/**
63 * omap_i2c_bus_setup - Process command line options for the I2C bus speed
64 * @str: String of options
65 *
66 * This function allow to override the default I2C bus speed for given I2C
67 * bus with a command line option.
68 *
69 * Format: i2c_bus=bus_id,clkrate (in kHz)
70 *
71 * Returns 1 on success, 0 otherwise.
72 */
73static int __init omap_i2c_bus_setup(char *str)
74{
75 int ports;
76 int ints[3];
77
78 ports = omap_i2c_nr_ports();
79 get_options(str, 3, ints);
80 if (ints[0] < 2 || ints[1] < 1 || ints[1] > ports)
81 return 0;
Kalle Jokiniemi20c9d2c2010-05-11 11:35:08 -070082 i2c_pdata[ints[1] - 1].clkrate = ints[2];
83 i2c_pdata[ints[1] - 1].clkrate |= OMAP_I2C_CMDLINE_SETUP;
Jarkko Nikula3a853fb2009-03-23 18:07:47 -070084
85 return 1;
86}
87__setup("i2c_bus=", omap_i2c_bus_setup);
88
Jarkko Nikula79547632009-03-23 18:07:48 -070089/*
90 * Register busses defined in command line but that are not registered with
91 * omap_register_i2c_bus from board initialization code.
92 */
93static int __init omap_register_i2c_bus_cmdline(void)
94{
95 int i, err = 0;
96
Kalle Jokiniemi20c9d2c2010-05-11 11:35:08 -070097 for (i = 0; i < ARRAY_SIZE(i2c_pdata); i++)
98 if (i2c_pdata[i].clkrate & OMAP_I2C_CMDLINE_SETUP) {
99 i2c_pdata[i].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
Tony Lindgren3a8761c2012-10-08 09:11:22 -0700100 err = omap_i2c_add_bus(&i2c_pdata[i], i + 1);
Jarkko Nikula79547632009-03-23 18:07:48 -0700101 if (err)
102 goto out;
103 }
104
105out:
106 return err;
107}
108subsys_initcall(omap_register_i2c_bus_cmdline);
109
Jarkko Nikulad4c58bf2009-03-23 18:07:46 -0700110/**
Jarkko Nikula9833eff2010-02-22 20:29:36 +0000111 * omap_register_i2c_bus - register I2C bus with device descriptors
Jarkko Nikulad4c58bf2009-03-23 18:07:46 -0700112 * @bus_id: bus id counting from number 1
113 * @clkrate: clock rate of the bus in kHz
114 * @info: pointer into I2C device descriptor table or NULL
115 * @len: number of descriptors in the table
116 *
117 * Returns 0 on success or an error code.
118 */
Jarkko Nikula9833eff2010-02-22 20:29:36 +0000119int __init omap_register_i2c_bus(int bus_id, u32 clkrate,
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200120 struct i2c_board_info const *info,
121 unsigned len)
122{
Jarkko Nikula3a853fb2009-03-23 18:07:47 -0700123 int err;
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200124
Jarkko Nikula3a853fb2009-03-23 18:07:47 -0700125 BUG_ON(bus_id < 1 || bus_id > omap_i2c_nr_ports());
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200126
127 if (info) {
128 err = i2c_register_board_info(bus_id, info, len);
129 if (err)
130 return err;
131 }
132
Kalle Jokiniemi20c9d2c2010-05-11 11:35:08 -0700133 if (!i2c_pdata[bus_id - 1].clkrate)
134 i2c_pdata[bus_id - 1].clkrate = clkrate;
135
136 i2c_pdata[bus_id - 1].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200137
Tony Lindgren3a8761c2012-10-08 09:11:22 -0700138 return omap_i2c_add_bus(&i2c_pdata[bus_id - 1], bus_id);
Jarkko Nikula85d05fb2007-11-07 06:54:31 +0200139}