185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/* 285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * Copyright (C) 2013 STMicroelectronics 385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * 485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * I2C master mode controller driver, used in STMicroelectronics devices. 585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * 685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * Author: Maxime Coquelin <maxime.coquelin@st.com> 785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * 885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * This program is free software; you can redistribute it and/or modify 985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * it under the terms of the GNU General Public License version 2, as 1085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * published by the Free Software Foundation. 1185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 1285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 1385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#include <linux/module.h> 1485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#include <linux/platform_device.h> 1585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#include <linux/i2c.h> 1685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#include <linux/clk.h> 1785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#include <linux/io.h> 1885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#include <linux/delay.h> 1985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#include <linux/interrupt.h> 2085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#include <linux/err.h> 2185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#include <linux/of.h> 2285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#include <linux/of_address.h> 2385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#include <linux/of_irq.h> 2485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 2585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/* SSC registers */ 2685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_BRG 0x000 2785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_TBUF 0x004 2885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_RBUF 0x008 2985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL 0x00C 3085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN 0x010 3185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA 0x014 3285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_I2C 0x018 3385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_SLAD 0x01C 3485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_REP_START_HOLD 0x020 3585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_START_HOLD 0x024 3685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_REP_START_SETUP 0x028 3785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_DATA_SETUP 0x02C 3885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STOP_SETUP 0x030 3985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_BUS_FREE 0x034 4085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_TX_FSTAT 0x038 4185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_RX_FSTAT 0x03C 4285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_PRE_SCALER_BRG 0x040 4385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CLR 0x080 4485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_NOISE_SUPP_WIDTH 0x100 4585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_PRSCALER 0x104 4685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_NOISE_SUPP_WIDTH_DATAOUT 0x108 4785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_PRSCALER_DATAOUT 0x10c 4885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 4985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/* SSC Control */ 5085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_DATA_WIDTH_9 0x8 5185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_DATA_WIDTH_MSK 0xf 5285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_BM 0xf 5385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_HB BIT(4) 5485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_PH BIT(5) 5585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_PO BIT(6) 5685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_SR BIT(7) 5785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_MS BIT(8) 5885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_EN BIT(9) 5985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_LPB BIT(10) 6085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_EN_TX_FIFO BIT(11) 6185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_EN_RX_FIFO BIT(12) 6285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CTL_EN_CLST_RX BIT(13) 6385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 6485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/* SSC Interrupt Enable */ 6585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN_RIEN BIT(0) 6685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN_TIEN BIT(1) 6785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN_TEEN BIT(2) 6885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN_REEN BIT(3) 6985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN_PEEN BIT(4) 7085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN_AASEN BIT(6) 7185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN_STOPEN BIT(7) 7285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN_ARBLEN BIT(8) 7385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN_NACKEN BIT(10) 7485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN_REPSTRTEN BIT(11) 7585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN_TX_FIFO_HALF BIT(12) 7685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_IEN_RX_FIFO_HALF_FULL BIT(14) 7785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 7885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/* SSC Status */ 7985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_RIR BIT(0) 8085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_TIR BIT(1) 8185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_TE BIT(2) 8285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_RE BIT(3) 8385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_PE BIT(4) 8485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_CLST BIT(5) 8585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_AAS BIT(6) 8685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_STOP BIT(7) 8785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_ARBL BIT(8) 8885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_BUSY BIT(9) 8985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_NACK BIT(10) 9085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_REPSTRT BIT(11) 9185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_TX_FIFO_HALF BIT(12) 9285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_TX_FIFO_FULL BIT(13) 9385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_STA_RX_FIFO_HALF BIT(14) 9485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 9585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/* SSC I2C Control */ 9685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_I2C_I2CM BIT(0) 9785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_I2C_STRTG BIT(1) 9885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_I2C_STOPG BIT(2) 9985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_I2C_ACKG BIT(3) 10085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_I2C_AD10 BIT(4) 10185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_I2C_TXENB BIT(5) 10285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_I2C_REPSTRTG BIT(11) 10385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_I2C_SLAVE_DISABLE BIT(12) 10485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 10585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/* SSC Tx FIFO Status */ 10685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_TX_FSTAT_STATUS 0x07 10785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 10885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/* SSC Rx FIFO Status */ 10985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_RX_FSTAT_STATUS 0x07 11085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 11185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/* SSC Clear bit operation */ 11285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CLR_SSCAAS BIT(6) 11385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CLR_SSCSTOP BIT(7) 11485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CLR_SSCARBL BIT(8) 11585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CLR_NACK BIT(10) 11685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_CLR_REPSTRT BIT(11) 11785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 11885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/* SSC Clock Prescaler */ 11985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_PRSC_VALUE 0x0f 12085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 12185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 12285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_TXFIFO_SIZE 0x8 12385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define SSC_RXFIFO_SIZE 0x8 12485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 12585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINenum st_i2c_mode { 12685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN I2C_MODE_STANDARD, 12785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN I2C_MODE_FAST, 12885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN I2C_MODE_END, 12985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN}; 13085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 13185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 13285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * struct st_i2c_timings - per-Mode tuning parameters 13385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @rate: I2C bus rate 13485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @rep_start_hold: I2C repeated start hold time requirement 13585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @rep_start_setup: I2C repeated start set up time requirement 13685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @start_hold: I2C start hold time requirement 13785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @data_setup_time: I2C data set up time requirement 13885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @stop_setup_time: I2C stop set up time requirement 13985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @bus_free_time: I2C bus free time requirement 14085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @sda_pulse_min_limit: I2C SDA pulse mini width limit 14185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 14285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstruct st_i2c_timings { 14385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 rate; 14485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 rep_start_hold; 14585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 rep_start_setup; 14685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 start_hold; 14785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 data_setup_time; 14885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 stop_setup_time; 14985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 bus_free_time; 15085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 sda_pulse_min_limit; 15185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN}; 15285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 15385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 15485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * struct st_i2c_client - client specific data 15585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @addr: 8-bit slave addr, including r/w bit 15685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @count: number of bytes to be transfered 15785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @xfered: number of bytes already transferred 15885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @buf: data buffer 15985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @result: result of the transfer 16085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @stop: last I2C msg to be sent, i.e. STOP to be generated 16185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 16285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstruct st_i2c_client { 16385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u8 addr; 16485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 count; 16585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 xfered; 16685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u8 *buf; 16785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN int result; 16885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN bool stop; 16985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN}; 17085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 17185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 17285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * struct st_i2c_dev - private data of the controller 17385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @adap: I2C adapter for this controller 17485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @dev: device for this controller 17585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @base: virtual memory area 17685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @complete: completion of I2C message 17785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @irq: interrupt line for th controller 17885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @clk: hw ssc block clock 17985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @mode: I2C mode of the controller. Standard or Fast only supported 18085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @scl_min_width_us: SCL line minimum pulse width in us 18185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @sda_min_width_us: SDA line minimum pulse width in us 18285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @client: I2C transfert information 18385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @busy: I2C transfer on-going 18485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 18585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstruct st_i2c_dev { 18685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct i2c_adapter adap; 18785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct device *dev; 18885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN void __iomem *base; 18985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct completion complete; 19085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN int irq; 19185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct clk *clk; 19285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN int mode; 19385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 scl_min_width_us; 19485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 sda_min_width_us; 19585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_client client; 19685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN bool busy; 19785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN}; 19885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 19985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic inline void st_i2c_set_bits(void __iomem *reg, u32 mask) 20085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 20185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(readl_relaxed(reg) | mask, reg); 20285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 20385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 20485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic inline void st_i2c_clr_bits(void __iomem *reg, u32 mask) 20585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 20685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(readl_relaxed(reg) & ~mask, reg); 20785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 20885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 20952976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN/* 21052976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN * From I2C Specifications v0.5. 21152976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN * 21252976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN * All the values below have +10% margin added to be 21352976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN * compatible with some out-of-spec devices, 21452976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN * like HDMI link of the Toshiba 19AV600 TV. 21552976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN */ 21685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic struct st_i2c_timings i2c_timings[] = { 21785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN [I2C_MODE_STANDARD] = { 21885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN .rate = 100000, 21952976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN .rep_start_hold = 4400, 22052976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN .rep_start_setup = 5170, 22152976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN .start_hold = 4400, 22252976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN .data_setup_time = 275, 22352976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN .stop_setup_time = 4400, 22452976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN .bus_free_time = 5170, 22585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN }, 22685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN [I2C_MODE_FAST] = { 22785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN .rate = 400000, 22852976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN .rep_start_hold = 660, 22952976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN .rep_start_setup = 660, 23052976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN .start_hold = 660, 23152976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN .data_setup_time = 110, 23252976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN .stop_setup_time = 660, 23352976fa74b9da790686ee116424458c238564cb1Maxime COQUELIN .bus_free_time = 1430, 23485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN }, 23585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN}; 23685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 23785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic void st_i2c_flush_rx_fifo(struct st_i2c_dev *i2c_dev) 23885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 23985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN int count, i; 24085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 24185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* 24285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * Counter only counts up to 7 but fifo size is 8... 24385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * When fifo is full, counter is 0 and RIR bit of status register is 24485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * set 24585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 24685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (readl_relaxed(i2c_dev->base + SSC_STA) & SSC_STA_RIR) 24785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN count = SSC_RXFIFO_SIZE; 24885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN else 24985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN count = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT) & 25085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN SSC_RX_FSTAT_STATUS; 25185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 25285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN for (i = 0; i < count; i++) 25385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN readl_relaxed(i2c_dev->base + SSC_RBUF); 25485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 25585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 25685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic void st_i2c_soft_reset(struct st_i2c_dev *i2c_dev) 25785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 25885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* 25985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * FIFO needs to be emptied before reseting the IP, 26085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * else the controller raises a BUSY error. 26185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 26285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_flush_rx_fifo(i2c_dev); 26385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 26485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_set_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR); 26585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_clr_bits(i2c_dev->base + SSC_CTL, SSC_CTL_SR); 26685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 26785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 26885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 26985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * st_i2c_hw_config() - Prepare SSC block, calculate and apply tuning timings 27085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @i2c_dev: Controller's private data 27185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 27285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic void st_i2c_hw_config(struct st_i2c_dev *i2c_dev) 27385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 27485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN unsigned long rate; 27585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 val, ns_per_clk; 27685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_timings *t = &i2c_timings[i2c_dev->mode]; 27785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 27885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_soft_reset(i2c_dev); 27985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 28085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN val = SSC_CLR_REPSTRT | SSC_CLR_NACK | SSC_CLR_SSCARBL | 28185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN SSC_CLR_SSCAAS | SSC_CLR_SSCSTOP; 28285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_CLR); 28385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 28485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* SSC Control register setup */ 28585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN val = SSC_CTL_PO | SSC_CTL_PH | SSC_CTL_HB | SSC_CTL_DATA_WIDTH_9; 28685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_CTL); 28785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 28885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN rate = clk_get_rate(i2c_dev->clk); 28985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ns_per_clk = 1000000000 / rate; 29085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 29185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Baudrate */ 29285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN val = rate / (2 * t->rate); 29385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_BRG); 29485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 29585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Pre-scaler baudrate */ 29685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(1, i2c_dev->base + SSC_PRE_SCALER_BRG); 29785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 29885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Enable I2C mode */ 29985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(SSC_I2C_I2CM, i2c_dev->base + SSC_I2C); 30085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 30185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Repeated start hold time */ 30285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN val = t->rep_start_hold / ns_per_clk; 30385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_REP_START_HOLD); 30485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 30585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Repeated start set up time */ 30685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN val = t->rep_start_setup / ns_per_clk; 30785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_REP_START_SETUP); 30885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 30985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Start hold time */ 31085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN val = t->start_hold / ns_per_clk; 31185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_START_HOLD); 31285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 31385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Data set up time */ 31485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN val = t->data_setup_time / ns_per_clk; 31585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_DATA_SETUP); 31685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 31785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Stop set up time */ 31885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN val = t->stop_setup_time / ns_per_clk; 31985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_STOP_SETUP); 32085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 32185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Bus free time */ 32285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN val = t->bus_free_time / ns_per_clk; 32385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_BUS_FREE); 32485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 32585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Prescalers set up */ 32685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN val = rate / 10000000; 32785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_PRSCALER); 32885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_PRSCALER_DATAOUT); 32985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 33085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Noise suppression witdh */ 33185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN val = i2c_dev->scl_min_width_us * rate / 100000000; 33285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH); 33385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 33485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Noise suppression max output data delay width */ 33585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN val = i2c_dev->sda_min_width_us * rate / 100000000; 33685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(val, i2c_dev->base + SSC_NOISE_SUPP_WIDTH_DATAOUT); 33785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 33885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 33985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic int st_i2c_wait_free_bus(struct st_i2c_dev *i2c_dev) 34085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 34185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 sta; 34285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN int i; 34385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 34485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN for (i = 0; i < 10; i++) { 34585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN sta = readl_relaxed(i2c_dev->base + SSC_STA); 34685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (!(sta & SSC_STA_BUSY)) 34785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return 0; 34885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 34985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN usleep_range(2000, 4000); 35085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 35185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 35285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_err(i2c_dev->dev, "bus not free (status = 0x%08x)\n", sta); 35385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 35485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return -EBUSY; 35585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 35685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 35785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 35885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * st_i2c_write_tx_fifo() - Write a byte in the Tx FIFO 35985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @i2c_dev: Controller's private data 36085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @byte: Data to write in the Tx FIFO 36185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 36285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic inline void st_i2c_write_tx_fifo(struct st_i2c_dev *i2c_dev, u8 byte) 36385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 36485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u16 tbuf = byte << 1; 36585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 36685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(tbuf | 1, i2c_dev->base + SSC_TBUF); 36785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 36885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 36985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 37085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * st_i2c_wr_fill_tx_fifo() - Fill the Tx FIFO in write mode 37185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @i2c_dev: Controller's private data 37285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * 37385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * This functions fills the Tx FIFO with I2C transfert buffer when 37485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * in write mode. 37585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 37685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic void st_i2c_wr_fill_tx_fifo(struct st_i2c_dev *i2c_dev) 37785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 37885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_client *c = &i2c_dev->client; 37985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 tx_fstat, sta; 38085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN int i; 38185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 38285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN sta = readl_relaxed(i2c_dev->base + SSC_STA); 38385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (sta & SSC_STA_TX_FIFO_FULL) 38485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return; 38585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 38685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT); 38785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN tx_fstat &= SSC_TX_FSTAT_STATUS; 38885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 38985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (c->count < (SSC_TXFIFO_SIZE - tx_fstat)) 39085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i = c->count; 39185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN else 39285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i = SSC_TXFIFO_SIZE - tx_fstat; 39385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 39485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN for (; i > 0; i--, c->count--, c->buf++) 39585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_write_tx_fifo(i2c_dev, *c->buf); 39685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 39785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 39885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 39985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * st_i2c_rd_fill_tx_fifo() - Fill the Tx FIFO in read mode 40085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @i2c_dev: Controller's private data 40185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * 40285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * This functions fills the Tx FIFO with fixed pattern when 40385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * in read mode to trigger clock. 40485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 40585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic void st_i2c_rd_fill_tx_fifo(struct st_i2c_dev *i2c_dev, int max) 40685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 40785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_client *c = &i2c_dev->client; 40885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 tx_fstat, sta; 40985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN int i; 41085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 41185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN sta = readl_relaxed(i2c_dev->base + SSC_STA); 41285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (sta & SSC_STA_TX_FIFO_FULL) 41385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return; 41485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 41585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN tx_fstat = readl_relaxed(i2c_dev->base + SSC_TX_FSTAT); 41685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN tx_fstat &= SSC_TX_FSTAT_STATUS; 41785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 41885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (max < (SSC_TXFIFO_SIZE - tx_fstat)) 41985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i = max; 42085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN else 42185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i = SSC_TXFIFO_SIZE - tx_fstat; 42285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 42385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN for (; i > 0; i--, c->xfered++) 42485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_write_tx_fifo(i2c_dev, 0xff); 42585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 42685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 42785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic void st_i2c_read_rx_fifo(struct st_i2c_dev *i2c_dev) 42885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 42985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_client *c = &i2c_dev->client; 43085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 i, sta; 43185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u16 rbuf; 43285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 43385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN sta = readl_relaxed(i2c_dev->base + SSC_STA); 43485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (sta & SSC_STA_RIR) { 43585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i = SSC_RXFIFO_SIZE; 43685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } else { 43785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i = readl_relaxed(i2c_dev->base + SSC_RX_FSTAT); 43885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i &= SSC_RX_FSTAT_STATUS; 43985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 44085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 44185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN for (; (i > 0) && (c->count > 0); i--, c->count--) { 44285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN rbuf = readl_relaxed(i2c_dev->base + SSC_RBUF) >> 1; 44385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN *c->buf++ = (u8)rbuf & 0xff; 44485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 44585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 44685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (i) { 44785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_err(i2c_dev->dev, "Unexpected %d bytes in rx fifo\n", i); 44885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_flush_rx_fifo(i2c_dev); 44985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 45085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 45185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 45285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 45385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * st_i2c_terminate_xfer() - Send either STOP or REPSTART condition 45485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @i2c_dev: Controller's private data 45585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 45685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic void st_i2c_terminate_xfer(struct st_i2c_dev *i2c_dev) 45785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 45885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_client *c = &i2c_dev->client; 45985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 46085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_clr_bits(i2c_dev->base + SSC_IEN, SSC_IEN_TEEN); 46185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG); 46285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 46385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (c->stop) { 46485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_STOPEN); 46585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); 46685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } else { 46785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_set_bits(i2c_dev->base + SSC_IEN, SSC_IEN_REPSTRTEN); 46885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_REPSTRTG); 46985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 47085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 47185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 47285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 47385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * st_i2c_handle_write() - Handle FIFO empty interrupt in case of write 47485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @i2c_dev: Controller's private data 47585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 47685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic void st_i2c_handle_write(struct st_i2c_dev *i2c_dev) 47785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 47885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_client *c = &i2c_dev->client; 47985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 48085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_flush_rx_fifo(i2c_dev); 48185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 48285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (!c->count) 48385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* End of xfer, send stop or repstart */ 48485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_terminate_xfer(i2c_dev); 48585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN else 48685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_wr_fill_tx_fifo(i2c_dev); 48785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 48885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 48985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 49085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * st_i2c_handle_write() - Handle FIFO enmpty interrupt in case of read 49185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @i2c_dev: Controller's private data 49285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 49385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic void st_i2c_handle_read(struct st_i2c_dev *i2c_dev) 49485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 49585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_client *c = &i2c_dev->client; 49685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 ien; 49785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 49885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Trash the address read back */ 49985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (!c->xfered) { 50085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN readl_relaxed(i2c_dev->base + SSC_RBUF); 50185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_TXENB); 50285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } else { 50385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_read_rx_fifo(i2c_dev); 50485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 50585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 50685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (!c->count) { 50785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* End of xfer, send stop or repstart */ 50885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_terminate_xfer(i2c_dev); 50985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } else if (c->count == 1) { 51085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Penultimate byte to xfer, disable ACK gen. */ 51185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_clr_bits(i2c_dev->base + SSC_I2C, SSC_I2C_ACKG); 51285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 51385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Last received byte is to be handled by NACK interrupt */ 51485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ien = SSC_IEN_NACKEN | SSC_IEN_ARBLEN; 51585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(ien, i2c_dev->base + SSC_IEN); 51685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 51785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_rd_fill_tx_fifo(i2c_dev, c->count); 51885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } else { 51985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_rd_fill_tx_fifo(i2c_dev, c->count - 1); 52085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 52185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 52285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 52385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 52485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * st_i2c_isr() - Interrupt routine 52585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @irq: interrupt number 52685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @data: Controller's private data 52785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 52885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic irqreturn_t st_i2c_isr_thread(int irq, void *data) 52985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 53085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_dev *i2c_dev = data; 53185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_client *c = &i2c_dev->client; 53285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 sta, ien; 53385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN int it; 53485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 53585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ien = readl_relaxed(i2c_dev->base + SSC_IEN); 53685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN sta = readl_relaxed(i2c_dev->base + SSC_STA); 53785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 53885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Use __fls() to check error bits first */ 53985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN it = __fls(sta & ien); 54085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (it < 0) { 54185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_dbg(i2c_dev->dev, "spurious it (sta=0x%04x, ien=0x%04x)\n", 54285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN sta, ien); 54385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return IRQ_NONE; 54485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 54585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 54685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN switch (1 << it) { 54785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN case SSC_STA_TE: 54885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (c->addr & I2C_M_RD) 54985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_handle_read(i2c_dev); 55085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN else 55185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_handle_write(i2c_dev); 55285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN break; 55385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 55485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN case SSC_STA_STOP: 55585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN case SSC_STA_REPSTRT: 55685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(0, i2c_dev->base + SSC_IEN); 55785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN complete(&i2c_dev->complete); 55885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN break; 55985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 56085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN case SSC_STA_NACK: 56185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(SSC_CLR_NACK, i2c_dev->base + SSC_CLR); 56285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 56385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Last received byte handled by NACK interrupt */ 56485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if ((c->addr & I2C_M_RD) && (c->count == 1) && (c->xfered)) { 56585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_handle_read(i2c_dev); 56685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN break; 56785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 56885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 56985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN it = SSC_IEN_STOPEN | SSC_IEN_ARBLEN; 57085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(it, i2c_dev->base + SSC_IEN); 57185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 57285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); 57385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN c->result = -EIO; 57485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN break; 57585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 57685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN case SSC_STA_ARBL: 57785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(SSC_CLR_SSCARBL, i2c_dev->base + SSC_CLR); 57885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 57985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN it = SSC_IEN_STOPEN | SSC_IEN_ARBLEN; 58085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(it, i2c_dev->base + SSC_IEN); 58185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 58285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STOPG); 5834fda99627dc037d3b316c3b3250075645cfcbe4dMaxime COQUELIN c->result = -EAGAIN; 58485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN break; 58585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 58685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN default: 58785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_err(i2c_dev->dev, 58885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN "it %d unhandled (sta=0x%04x)\n", it, sta); 58985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 59085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 59185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* 59285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * Read IEN register to ensure interrupt mask write is effective 59385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * before re-enabling interrupt at GIC level, and thus avoid spurious 59485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * interrupts. 59585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 59685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN readl(i2c_dev->base + SSC_IEN); 59785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 59885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return IRQ_HANDLED; 59985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 60085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 60185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 60285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * st_i2c_xfer_msg() - Transfer a single I2C message 60385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @i2c_dev: Controller's private data 60485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @msg: I2C message to transfer 60585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @is_first: first message of the sequence 60685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @is_last: last message of the sequence 60785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 60885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic int st_i2c_xfer_msg(struct st_i2c_dev *i2c_dev, struct i2c_msg *msg, 60985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN bool is_first, bool is_last) 61085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 61185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_client *c = &i2c_dev->client; 61285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 ctl, i2c, it; 61385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN unsigned long timeout; 61485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN int ret; 61585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 61685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN c->addr = (u8)(msg->addr << 1); 61785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN c->addr |= (msg->flags & I2C_M_RD); 61885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN c->buf = msg->buf; 61985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN c->count = msg->len; 62085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN c->xfered = 0; 62185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN c->result = 0; 62285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN c->stop = is_last; 62385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 62413509c3a9d20a9df93dc9b944e8bd20fe1b454a7Linus Torvalds reinit_completion(&i2c_dev->complete); 62585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 62685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ctl = SSC_CTL_EN | SSC_CTL_MS | SSC_CTL_EN_RX_FIFO | SSC_CTL_EN_TX_FIFO; 62785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_set_bits(i2c_dev->base + SSC_CTL, ctl); 62885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 62985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c = SSC_I2C_TXENB; 63085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (c->addr & I2C_M_RD) 63185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c |= SSC_I2C_ACKG; 63285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_set_bits(i2c_dev->base + SSC_I2C, i2c); 63385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 63485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Write slave address */ 63585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_write_tx_fifo(i2c_dev, c->addr); 63685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 63785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Pre-fill Tx fifo with data in case of write */ 63885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (!(c->addr & I2C_M_RD)) 63985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_wr_fill_tx_fifo(i2c_dev); 64085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 64185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN it = SSC_IEN_NACKEN | SSC_IEN_TEEN | SSC_IEN_ARBLEN; 64285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(it, i2c_dev->base + SSC_IEN); 64385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 64485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (is_first) { 64585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ret = st_i2c_wait_free_bus(i2c_dev); 64685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (ret) 64785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return ret; 64885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 64985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_set_bits(i2c_dev->base + SSC_I2C, SSC_I2C_STRTG); 65085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 65185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 65285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN timeout = wait_for_completion_timeout(&i2c_dev->complete, 65385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_dev->adap.timeout); 65485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ret = c->result; 65585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 65685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (!timeout) { 65785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_err(i2c_dev->dev, "Write to slave 0x%x timed out\n", 65885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN c->addr); 65985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ret = -ETIMEDOUT; 66085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 66185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 66285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c = SSC_I2C_STOPG | SSC_I2C_REPSTRTG; 66385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_clr_bits(i2c_dev->base + SSC_I2C, i2c); 66485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 66585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN writel_relaxed(SSC_CLR_SSCSTOP | SSC_CLR_REPSTRT, 66685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_dev->base + SSC_CLR); 66785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 66885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return ret; 66985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 67085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 67185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN/** 67285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * st_i2c_xfer() - Transfer a single I2C message 67385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @i2c_adap: Adapter pointer to the controller 67485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @msgs: Pointer to data to be written. 67585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN * @num: Number of messages to be executed 67685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN */ 67785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic int st_i2c_xfer(struct i2c_adapter *i2c_adap, 67885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct i2c_msg msgs[], int num) 67985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 68085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_dev *i2c_dev = i2c_get_adapdata(i2c_adap); 68185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN int ret, i; 68285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 68385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_dev->busy = true; 68485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 68585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ret = clk_prepare_enable(i2c_dev->clk); 68685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (ret) { 68785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_err(i2c_dev->dev, "Failed to prepare_enable clock\n"); 68885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return ret; 68985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 69085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 69185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN pinctrl_pm_select_default_state(i2c_dev->dev); 69285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 69385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN st_i2c_hw_config(i2c_dev); 69485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 69585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN for (i = 0; (i < num) && !ret; i++) 69685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ret = st_i2c_xfer_msg(i2c_dev, &msgs[i], i == 0, i == num - 1); 69785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 69885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN pinctrl_pm_select_idle_state(i2c_dev->dev); 69985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 70085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN clk_disable_unprepare(i2c_dev->clk); 70185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 70285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_dev->busy = false; 70385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 70485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return (ret < 0) ? ret : i; 70585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 70685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 70785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#ifdef CONFIG_PM_SLEEP 70885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic int st_i2c_suspend(struct device *dev) 70985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 71085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct platform_device *pdev = 71185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN container_of(dev, struct platform_device, dev); 71285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev); 71385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 71485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (i2c_dev->busy) 71585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return -EBUSY; 71685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 71785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN pinctrl_pm_select_sleep_state(dev); 71885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 71985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return 0; 72085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 72185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 72285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic int st_i2c_resume(struct device *dev) 72385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 72485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN pinctrl_pm_select_default_state(dev); 72585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* Go in idle state if available */ 72685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN pinctrl_pm_select_idle_state(dev); 72785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 72885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return 0; 72985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 73085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 73185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic SIMPLE_DEV_PM_OPS(st_i2c_pm, st_i2c_suspend, st_i2c_resume); 73285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define ST_I2C_PM (&st_i2c_pm) 73385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#else 73485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#define ST_I2C_PM NULL 73585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN#endif 73685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 73785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic u32 st_i2c_func(struct i2c_adapter *adap) 73885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 73985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 74085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 74185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 74285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic struct i2c_algorithm st_i2c_algo = { 74385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN .master_xfer = st_i2c_xfer, 74485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN .functionality = st_i2c_func, 74585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN}; 74685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 74785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic int st_i2c_of_get_deglitch(struct device_node *np, 74885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_dev *i2c_dev) 74985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 75085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN int ret; 75185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 75285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ret = of_property_read_u32(np, "st,i2c-min-scl-pulse-width-us", 75385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN &i2c_dev->scl_min_width_us); 75485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if ((ret == -ENODATA) || (ret == -EOVERFLOW)) { 75585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_err(i2c_dev->dev, "st,i2c-min-scl-pulse-width-us invalid\n"); 75685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return ret; 75785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 75885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 75985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ret = of_property_read_u32(np, "st,i2c-min-sda-pulse-width-us", 76085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN &i2c_dev->sda_min_width_us); 76185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if ((ret == -ENODATA) || (ret == -EOVERFLOW)) { 76285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_err(i2c_dev->dev, "st,i2c-min-sda-pulse-width-us invalid\n"); 76385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return ret; 76485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 76585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 76685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return 0; 76785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 76885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 76985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic int st_i2c_probe(struct platform_device *pdev) 77085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 77185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct device_node *np = pdev->dev.of_node; 77285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_dev *i2c_dev; 77385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct resource *res; 77485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN u32 clk_rate; 77585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct i2c_adapter *adap; 77685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN int ret; 77785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 77885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_dev = devm_kzalloc(&pdev->dev, sizeof(*i2c_dev), GFP_KERNEL); 77985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (!i2c_dev) 78085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return -ENOMEM; 78185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 78285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 78385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_dev->base = devm_ioremap_resource(&pdev->dev, res); 78485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (IS_ERR(i2c_dev->base)) 78585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return PTR_ERR(i2c_dev->base); 78685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 78785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_dev->irq = irq_of_parse_and_map(np, 0); 78885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (!i2c_dev->irq) { 78985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_err(&pdev->dev, "IRQ missing or invalid\n"); 79085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return -EINVAL; 79185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 79285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 79385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_dev->clk = of_clk_get_by_name(np, "ssc"); 79485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (IS_ERR(i2c_dev->clk)) { 79585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_err(&pdev->dev, "Unable to request clock\n"); 79685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return PTR_ERR(i2c_dev->clk); 79785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 79885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 79985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_dev->mode = I2C_MODE_STANDARD; 80085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ret = of_property_read_u32(np, "clock-frequency", &clk_rate); 80185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if ((!ret) && (clk_rate == 400000)) 80285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_dev->mode = I2C_MODE_FAST; 80385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 80485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_dev->dev = &pdev->dev; 80585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 80685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ret = devm_request_threaded_irq(&pdev->dev, i2c_dev->irq, 80785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN NULL, st_i2c_isr_thread, 80885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN IRQF_ONESHOT, pdev->name, i2c_dev); 80985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (ret) { 81085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_err(&pdev->dev, "Failed to request irq %i\n", i2c_dev->irq); 81185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return ret; 81285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 81385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 81485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN pinctrl_pm_select_default_state(i2c_dev->dev); 81585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN /* In case idle state available, select it */ 81685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN pinctrl_pm_select_idle_state(i2c_dev->dev); 81785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 81885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ret = st_i2c_of_get_deglitch(np, i2c_dev); 81985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (ret) 82085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return ret; 82185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 82285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN adap = &i2c_dev->adap; 82385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_set_adapdata(adap, i2c_dev); 8246d9fe5884e8dbf31a550316d924bea8c3ed155cfFabio Estevam snprintf(adap->name, sizeof(adap->name), "ST I2C(0x%pa)", &res->start); 82585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN adap->owner = THIS_MODULE; 82685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN adap->timeout = 2 * HZ; 82785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN adap->retries = 0; 82885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN adap->algo = &st_i2c_algo; 82985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN adap->dev.parent = &pdev->dev; 83085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN adap->dev.of_node = pdev->dev.of_node; 83185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 83285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN init_completion(&i2c_dev->complete); 83385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 83485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN ret = i2c_add_adapter(adap); 83585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN if (ret) { 83685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_err(&pdev->dev, "Failed to add adapter\n"); 83785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return ret; 83885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN } 83985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 84085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN platform_set_drvdata(pdev, i2c_dev); 84185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 84285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN dev_info(i2c_dev->dev, "%s initialized\n", adap->name); 84385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 84485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return 0; 84585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 84685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 84785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic int st_i2c_remove(struct platform_device *pdev) 84885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN{ 84985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN struct st_i2c_dev *i2c_dev = platform_get_drvdata(pdev); 85085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 85185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN i2c_del_adapter(&i2c_dev->adap); 85285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 85385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN return 0; 85485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN} 85585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 856eae45e5dd229d5c6f0aed9789a5d1e84f7ea6100Jingoo Hanstatic const struct of_device_id st_i2c_match[] = { 85785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN { .compatible = "st,comms-ssc-i2c", }, 85885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN { .compatible = "st,comms-ssc4-i2c", }, 85985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN {}, 86085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN}; 86185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINMODULE_DEVICE_TABLE(of, st_i2c_match); 86285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 86385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINstatic struct platform_driver st_i2c_driver = { 86485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN .driver = { 86585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN .name = "st-i2c", 86685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN .owner = THIS_MODULE, 86785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN .of_match_table = st_i2c_match, 86885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN .pm = ST_I2C_PM, 86985b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN }, 87085b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN .probe = st_i2c_probe, 87185b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN .remove = st_i2c_remove, 87285b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN}; 87385b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 87485b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINmodule_platform_driver(st_i2c_driver); 87585b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELIN 87685b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINMODULE_AUTHOR("Maxime Coquelin <maxime.coquelin@st.com>"); 87785b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINMODULE_DESCRIPTION("STMicroelectronics I2C driver"); 87885b4fab2696000369176dd222dc3001d4a7e0fa6Maxime COQUELINMODULE_LICENSE("GPL v2"); 879