Ejemplo n.º 1
0
static void stmpe811_i2c_init()
{
	rcc_periph_clock_enable(RCC_GPIOA);
	rcc_periph_clock_enable(RCC_GPIOC);
	rcc_periph_clock_enable(RCC_I2C3);

	gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8);
	gpio_set_output_options(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,
		GPIO8);
	gpio_set_af(GPIOA, GPIO_AF4, GPIO8);

	gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9);
	gpio_set_output_options(GPIOC, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,
		GPIO9);
	gpio_set_af(GPIOC, GPIO_AF4, GPIO9);

	i2c_peripheral_disable(I2C3); /* disable i2c during setup */
	i2c_reset(I2C3);

	//i2c_set_fast_mode(I2C3);
	//i2c_set_clock_frequency(I2C3, I2C_CR2_FREQ_42MHZ);
	//i2c_set_ccr(I2C3, 35);
	//i2c_set_trise(I2C3, 43);
	i2c_set_speed(I2C3, 0);

	i2c_peripheral_enable(I2C3); /* finally enable i2c */

	i2c_set_own_7bit_slave_address(I2C3, 0x00);
}
Ejemplo n.º 2
0
void mcp4725_init(mcp4725 *dev, uint8_t dev_addr, i2c_port port, i2c_speed speed) {
	// Force degrade invalid speed mode.
	if(speed == I2C_FAST_MODE_PLUS)
		speed = I2C_FAST_MODE;
	
	// Store the value in the object.
	dev->addr = dev_addr; //(DEVICE_CODE << 4) | (dev_addr << 1);
	dev->port = port;
	i2c_set_speed(dev->port, speed);
	
	i2c_init(dev->port);
}
Ejemplo n.º 3
0
static int i2c_jz_probe(struct platform_device *dev)
{
    int ret = 0;
    struct i2c_jz *i2c;
    struct resource *res;
    unsigned int reg_tmp;

    i2c = kzalloc(sizeof(struct i2c_jz), GFP_KERNEL);
    if (!i2c) {
        printk("Error: Now we can not malloc memory for I2C!\n");
        ret = -ENOMEM;
        goto ERR0;
    }

    i2c->adap.owner = THIS_MODULE;
    i2c->adap.algo = &i2c_jz_algorithm;
    i2c->adap.retries = 5;
    i2c->adap.timeout = 5;
    i2c->adap.algo_data = i2c;
    i2c->adap.dev.parent = &dev->dev;
    i2c->adap.nr = dev->id;
    sprintf(i2c->adap.name, "i2c%u", dev->id);

    i2c->clk = clk_get(&dev->dev, i2c->adap.name);
    if (!i2c->clk) {
        printk("Error: Now we can not get i2c%d clock!\n", dev->id);
        ret = -ENODEV;
        goto clk_failed;
    }

    res = platform_get_resource(dev, IORESOURCE_MEM, 0);
    i2c->iomem = ioremap(res->start, resource_size(res));
    if (!i2c->iomem) {
        printk("Error: Now we can remap IO for I2C%d!\n", dev->id);
        ret = -ENOMEM;
        goto io_failed;
    }

    i2c->irq = platform_get_irq(dev, 0);
    ret =
        request_irq(i2c->irq, i2c_jz_irq, IRQF_DISABLED,
                    dev_name(&dev->dev), i2c);
    if (ret) {
        printk("Error: Now we can request irq for I2C%d!\n", dev->id);
        ret = -ENODEV;
        goto irq_failed;
    }

    clk_enable(i2c->clk);

    res = platform_get_resource(dev, IORESOURCE_BUS, 0);

    i2c_set_speed(i2c, res->start * 1000);

#if 0
    reg_tmp = i2c_readl(i2c, I2C_DC);
    reg_tmp &= ~I2C_DC_STP;
    i2c_writel(i2c, I2C_DC, reg_tmp);
#endif

    reg_tmp = i2c_readl(i2c, I2C_CTRL);
    reg_tmp |= I2C_CTRL_REST;
    i2c_writel(i2c, I2C_CTRL, reg_tmp);

    // for jgao WHY?
    //  i2c_writel(i2c, I2C_FLT, 0xF);      /*set filter*/

    i2c_writel(i2c, I2C_INTM, 0x0);

    init_completion(&i2c->complete);

    ret = i2c_add_numbered_adapter(&i2c->adap);
    if (ret < 0) {
        dev_err(&(i2c->adap.dev), KERN_INFO "I2C: Failed to add bus\n");
        goto adapt_failed;
    }

    platform_set_drvdata(dev, i2c);

    i2c_jz_enable(i2c);

    clk_disable(i2c->clk);

#ifdef CONFIG_I2C_DEBUG_INFO
    ret = create_debug_sysfs_interface(&dev->dev);
    if (ret < 0)
        dev_err(&i2c->adap.dev, "create debug sysfs interface failed\n");
#endif

    return 0;

adapt_failed:
    free_irq(i2c->irq, i2c);
irq_failed:
    iounmap(i2c->iomem);
io_failed:
    clk_put(i2c->clk);
clk_failed:
    kfree(i2c);
ERR0:
    return ret;
}