Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Helper module for board specific I2C bus registration
0004  *
0005  * Copyright (C) 2009 Nokia Corporation.
0006  */
0007 
0008 #include <linux/i2c.h>
0009 #include <linux/platform_data/i2c-omap.h>
0010 
0011 #include "mux.h"
0012 #include "soc.h"
0013 #include "i2c.h"
0014 
0015 #define OMAP_I2C_SIZE       0x3f
0016 #define OMAP1_I2C_BASE      0xfffb3800
0017 
0018 static const char name[] = "omap_i2c";
0019 
0020 static struct resource i2c_resources[2] = {
0021 };
0022 
0023 static struct platform_device omap_i2c_devices[1] = {
0024 };
0025 
0026 static void __init omap1_i2c_mux_pins(int bus_id)
0027 {
0028     if (cpu_is_omap7xx()) {
0029         omap_cfg_reg(I2C_7XX_SDA);
0030         omap_cfg_reg(I2C_7XX_SCL);
0031     } else {
0032         omap_cfg_reg(I2C_SDA);
0033         omap_cfg_reg(I2C_SCL);
0034     }
0035 }
0036 
0037 int __init omap_i2c_add_bus(struct omap_i2c_bus_platform_data *pdata,
0038                 int bus_id)
0039 {
0040     struct platform_device *pdev;
0041     struct resource *res;
0042 
0043     if (bus_id > 1)
0044         return -EINVAL;
0045 
0046     omap1_i2c_mux_pins(bus_id);
0047 
0048     pdev = &omap_i2c_devices[bus_id - 1];
0049     pdev->id = bus_id;
0050     pdev->name = name;
0051     pdev->num_resources = ARRAY_SIZE(i2c_resources);
0052     res = i2c_resources;
0053     res[0].start = OMAP1_I2C_BASE;
0054     res[0].end = res[0].start + OMAP_I2C_SIZE;
0055     res[0].flags = IORESOURCE_MEM;
0056     res[1].start = INT_I2C;
0057     res[1].flags = IORESOURCE_IRQ;
0058     pdev->resource = res;
0059 
0060     /* all OMAP1 have IP version 1 register set */
0061     pdata->rev = OMAP_I2C_IP_VERSION_1;
0062 
0063     /* all OMAP1 I2C are implemented like this */
0064     pdata->flags = OMAP_I2C_FLAG_NO_FIFO |
0065                OMAP_I2C_FLAG_SIMPLE_CLOCK |
0066                OMAP_I2C_FLAG_16BIT_DATA_REG |
0067                OMAP_I2C_FLAG_ALWAYS_ARMXOR_CLK;
0068 
0069     /* how the cpu bus is wired up differs for 7xx only */
0070 
0071     if (cpu_is_omap7xx())
0072         pdata->flags |= OMAP_I2C_FLAG_BUS_SHIFT_1;
0073     else
0074         pdata->flags |= OMAP_I2C_FLAG_BUS_SHIFT_2;
0075 
0076     pdev->dev.platform_data = pdata;
0077 
0078     return platform_device_register(pdev);
0079 }
0080 
0081 #define OMAP_I2C_MAX_CONTROLLERS 4
0082 static struct omap_i2c_bus_platform_data i2c_pdata[OMAP_I2C_MAX_CONTROLLERS];
0083 
0084 #define OMAP_I2C_CMDLINE_SETUP  (BIT(31))
0085 
0086 /**
0087  * omap_i2c_bus_setup - Process command line options for the I2C bus speed
0088  * @str: String of options
0089  *
0090  * This function allow to override the default I2C bus speed for given I2C
0091  * bus with a command line option.
0092  *
0093  * Format: i2c_bus=bus_id,clkrate (in kHz)
0094  *
0095  * Returns 1 on success, 0 otherwise.
0096  */
0097 static int __init omap_i2c_bus_setup(char *str)
0098 {
0099     int ints[3];
0100 
0101     get_options(str, 3, ints);
0102     if (ints[0] < 2 || ints[1] < 1 ||
0103             ints[1] > OMAP_I2C_MAX_CONTROLLERS)
0104         return 0;
0105     i2c_pdata[ints[1] - 1].clkrate = ints[2];
0106     i2c_pdata[ints[1] - 1].clkrate |= OMAP_I2C_CMDLINE_SETUP;
0107 
0108     return 1;
0109 }
0110 __setup("i2c_bus=", omap_i2c_bus_setup);
0111 
0112 /*
0113  * Register busses defined in command line but that are not registered with
0114  * omap_register_i2c_bus from board initialization code.
0115  */
0116 int __init omap_register_i2c_bus_cmdline(void)
0117 {
0118     int i, err = 0;
0119 
0120     for (i = 0; i < ARRAY_SIZE(i2c_pdata); i++)
0121         if (i2c_pdata[i].clkrate & OMAP_I2C_CMDLINE_SETUP) {
0122             i2c_pdata[i].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
0123             err = omap_i2c_add_bus(&i2c_pdata[i], i + 1);
0124             if (err)
0125                 goto out;
0126         }
0127 
0128 out:
0129     return err;
0130 }
0131 
0132 /**
0133  * omap_register_i2c_bus - register I2C bus with device descriptors
0134  * @bus_id: bus id counting from number 1
0135  * @clkrate: clock rate of the bus in kHz
0136  * @info: pointer into I2C device descriptor table or NULL
0137  * @len: number of descriptors in the table
0138  *
0139  * Returns 0 on success or an error code.
0140  */
0141 int __init omap_register_i2c_bus(int bus_id, u32 clkrate,
0142               struct i2c_board_info const *info,
0143               unsigned len)
0144 {
0145     int err;
0146 
0147     BUG_ON(bus_id < 1 || bus_id > OMAP_I2C_MAX_CONTROLLERS);
0148 
0149     if (info) {
0150         err = i2c_register_board_info(bus_id, info, len);
0151         if (err)
0152             return err;
0153     }
0154 
0155     if (!i2c_pdata[bus_id - 1].clkrate)
0156         i2c_pdata[bus_id - 1].clkrate = clkrate;
0157 
0158     i2c_pdata[bus_id - 1].clkrate &= ~OMAP_I2C_CMDLINE_SETUP;
0159 
0160     return omap_i2c_add_bus(&i2c_pdata[bus_id - 1], bus_id);
0161 }
0162 
0163 static  int __init omap_i2c_cmdline(void)
0164 {
0165     return omap_register_i2c_bus_cmdline();
0166 }
0167 subsys_initcall(omap_i2c_cmdline);