Jarkko Nikula | 85d05fb | 2007-11-07 06:54:31 +0200 | [diff] [blame] | 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 | * |
Jarkko Nikula | ddf25df | 2009-05-25 11:08:43 -0700 | [diff] [blame] | 8 | * Contact: Jarkko Nikula <jhnikula@gmail.com> |
Jarkko Nikula | 85d05fb | 2007-11-07 06:54:31 +0200 | [diff] [blame] | 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> |
Tony Lindgren | 3a8761c | 2012-10-08 09:11:22 -0700 | [diff] [blame] | 29 | #include <linux/i2c-omap.h> |
Paul Walmsley | 4d17aeb | 2010-09-21 19:37:15 +0530 | [diff] [blame] | 30 | #include <linux/slab.h> |
| 31 | #include <linux/err.h> |
| 32 | #include <linux/clk.h> |
Kalle Jokiniemi | 20c9d2c | 2010-05-11 11:35:08 -0700 | [diff] [blame] | 33 | |
Russell King | 80b02c1 | 2009-01-08 10:01:47 +0000 | [diff] [blame] | 34 | #include <mach/irqs.h> |
Tony Lindgren | e4c060d | 2012-10-05 13:25:59 -0700 | [diff] [blame] | 35 | |
| 36 | #include "../mach-omap1/soc.h" |
| 37 | #include "../mach-omap2/soc.h" |
Jarkko Nikula | 85d05fb | 2007-11-07 06:54:31 +0200 | [diff] [blame] | 38 | |
Tony Lindgren | 3a8761c | 2012-10-08 09:11:22 -0700 | [diff] [blame] | 39 | #include "i2c.h" |
Jarkko Nikula | 85d05fb | 2007-11-07 06:54:31 +0200 | [diff] [blame] | 40 | |
Paul Walmsley | 4d17aeb | 2010-09-21 19:37:15 +0530 | [diff] [blame] | 41 | #define OMAP_I2C_MAX_CONTROLLERS 4 |
| 42 | static struct omap_i2c_bus_platform_data i2c_pdata[OMAP_I2C_MAX_CONTROLLERS]; |
Jarkko Nikula | 85d05fb | 2007-11-07 06:54:31 +0200 | [diff] [blame] | 43 | |
Jarkko Nikula | 7954763 | 2009-03-23 18:07:48 -0700 | [diff] [blame] | 44 | #define OMAP_I2C_CMDLINE_SETUP (BIT(31)) |
| 45 | |
Jarkko Nikula | 3a853fb | 2009-03-23 18:07:47 -0700 | [diff] [blame] | 46 | static 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 Lindgren | 6daa642 | 2010-05-20 11:36:43 -0700 | [diff] [blame] | 56 | else if (cpu_is_omap44xx()) |
| 57 | ports = 4; |
Jarkko Nikula | 3a853fb | 2009-03-23 18:07:47 -0700 | [diff] [blame] | 58 | |
| 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 | */ |
| 73 | static 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 Jokiniemi | 20c9d2c | 2010-05-11 11:35:08 -0700 | [diff] [blame] | 82 | i2c_pdata[ints[1] - 1].clkrate = ints[2]; |
| 83 | i2c_pdata[ints[1] - 1].clkrate |= OMAP_I2C_CMDLINE_SETUP; |
Jarkko Nikula | 3a853fb | 2009-03-23 18:07:47 -0700 | [diff] [blame] | 84 | |
| 85 | return 1; |
| 86 | } |
| 87 | __setup("i2c_bus=", omap_i2c_bus_setup); |
| 88 | |
Jarkko Nikula | 7954763 | 2009-03-23 18:07:48 -0700 | [diff] [blame] | 89 | /* |
| 90 | * Register busses defined in command line but that are not registered with |
| 91 | * omap_register_i2c_bus from board initialization code. |
| 92 | */ |
| 93 | static int __init omap_register_i2c_bus_cmdline(void) |
| 94 | { |
| 95 | int i, err = 0; |
| 96 | |
Kalle Jokiniemi | 20c9d2c | 2010-05-11 11:35:08 -0700 | [diff] [blame] | 97 | 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 Lindgren | 3a8761c | 2012-10-08 09:11:22 -0700 | [diff] [blame] | 100 | err = omap_i2c_add_bus(&i2c_pdata[i], i + 1); |
Jarkko Nikula | 7954763 | 2009-03-23 18:07:48 -0700 | [diff] [blame] | 101 | if (err) |
| 102 | goto out; |
| 103 | } |
| 104 | |
| 105 | out: |
| 106 | return err; |
| 107 | } |
| 108 | subsys_initcall(omap_register_i2c_bus_cmdline); |
| 109 | |
Jarkko Nikula | d4c58bf | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 110 | /** |
Jarkko Nikula | 9833eff | 2010-02-22 20:29:36 +0000 | [diff] [blame] | 111 | * omap_register_i2c_bus - register I2C bus with device descriptors |
Jarkko Nikula | d4c58bf | 2009-03-23 18:07:46 -0700 | [diff] [blame] | 112 | * @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 Nikula | 9833eff | 2010-02-22 20:29:36 +0000 | [diff] [blame] | 119 | int __init omap_register_i2c_bus(int bus_id, u32 clkrate, |
Jarkko Nikula | 85d05fb | 2007-11-07 06:54:31 +0200 | [diff] [blame] | 120 | struct i2c_board_info const *info, |
| 121 | unsigned len) |
| 122 | { |
Jarkko Nikula | 3a853fb | 2009-03-23 18:07:47 -0700 | [diff] [blame] | 123 | int err; |
Jarkko Nikula | 85d05fb | 2007-11-07 06:54:31 +0200 | [diff] [blame] | 124 | |
Jarkko Nikula | 3a853fb | 2009-03-23 18:07:47 -0700 | [diff] [blame] | 125 | BUG_ON(bus_id < 1 || bus_id > omap_i2c_nr_ports()); |
Jarkko Nikula | 85d05fb | 2007-11-07 06:54:31 +0200 | [diff] [blame] | 126 | |
| 127 | if (info) { |
| 128 | err = i2c_register_board_info(bus_id, info, len); |
| 129 | if (err) |
| 130 | return err; |
| 131 | } |
| 132 | |
Kalle Jokiniemi | 20c9d2c | 2010-05-11 11:35:08 -0700 | [diff] [blame] | 133 | 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 Nikula | 85d05fb | 2007-11-07 06:54:31 +0200 | [diff] [blame] | 137 | |
Tony Lindgren | 3a8761c | 2012-10-08 09:11:22 -0700 | [diff] [blame] | 138 | return omap_i2c_add_bus(&i2c_pdata[bus_id - 1], bus_id); |
Jarkko Nikula | 85d05fb | 2007-11-07 06:54:31 +0200 | [diff] [blame] | 139 | } |