1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465 |
- #include <linux/bitops.h>
- #include <linux/clk.h>
- #include <linux/completion.h>
- #include <linux/err.h>
- #include <linux/i2c.h>
- #include <linux/init.h>
- #include <linux/interrupt.h>
- #include <linux/io.h>
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/of_platform.h>
- #include <linux/platform_device.h>
- #include <linux/slab.h>
- #include <linux/timer.h>
- #define SCB_STATUS_REG 0x00
- #define SCB_OVERRIDE_REG 0x04
- #define SCB_READ_ADDR_REG 0x08
- #define SCB_READ_COUNT_REG 0x0c
- #define SCB_WRITE_ADDR_REG 0x10
- #define SCB_READ_DATA_REG 0x14
- #define SCB_WRITE_DATA_REG 0x18
- #define SCB_FIFO_STATUS_REG 0x1c
- #define SCB_CONTROL_SOFT_RESET 0x1f
- #define SCB_CLK_SET_REG 0x3c
- #define SCB_INT_STATUS_REG 0x40
- #define SCB_INT_CLEAR_REG 0x44
- #define SCB_INT_MASK_REG 0x48
- #define SCB_CONTROL_REG 0x4c
- #define SCB_TIME_TPL_REG 0x50
- #define SCB_TIME_TPH_REG 0x54
- #define SCB_TIME_TP2S_REG 0x58
- #define SCB_TIME_TBI_REG 0x60
- #define SCB_TIME_TSL_REG 0x64
- #define SCB_TIME_TDL_REG 0x68
- #define SCB_TIME_TSDL_REG 0x6c
- #define SCB_TIME_TSDH_REG 0x70
- #define SCB_READ_XADDR_REG 0x74
- #define SCB_WRITE_XADDR_REG 0x78
- #define SCB_WRITE_COUNT_REG 0x7c
- #define SCB_CORE_REV_REG 0x80
- #define SCB_TIME_TCKH_REG 0x84
- #define SCB_TIME_TCKL_REG 0x88
- #define SCB_FIFO_FLUSH_REG 0x8c
- #define SCB_READ_FIFO_REG 0x94
- #define SCB_CLEAR_REG 0x98
- #define SCB_CONTROL_CLK_ENABLE 0x1e0
- #define SCB_CONTROL_TRANSACTION_HALT 0x200
- #define FIFO_READ_FULL BIT(0)
- #define FIFO_READ_EMPTY BIT(1)
- #define FIFO_WRITE_FULL BIT(2)
- #define FIFO_WRITE_EMPTY BIT(3)
- #define SCB_FILT_DISABLE BIT(31)
- #define SCB_FILT_BYPASS BIT(30)
- #define SCB_FILT_INC_MASK 0x7f
- #define SCB_FILT_INC_SHIFT 16
- #define SCB_INC_MASK 0x7f
- #define SCB_INC_SHIFT 8
- #define INT_BUS_INACTIVE BIT(0)
- #define INT_UNEXPECTED_START BIT(1)
- #define INT_SCLK_LOW_TIMEOUT BIT(2)
- #define INT_SDAT_LOW_TIMEOUT BIT(3)
- #define INT_WRITE_ACK_ERR BIT(4)
- #define INT_ADDR_ACK_ERR BIT(5)
- #define INT_FIFO_FULL BIT(9)
- #define INT_FIFO_FILLING BIT(10)
- #define INT_FIFO_EMPTY BIT(11)
- #define INT_FIFO_EMPTYING BIT(12)
- #define INT_TRANSACTION_DONE BIT(15)
- #define INT_SLAVE_EVENT BIT(16)
- #define INT_MASTER_HALTED BIT(17)
- #define INT_TIMING BIT(18)
- #define INT_STOP_DETECTED BIT(19)
- #define INT_FIFO_FULL_FILLING (INT_FIFO_FULL | INT_FIFO_FILLING)
- #define INT_LEVEL 0x01e00
- #define INT_ENABLE_MASK_INACTIVE 0x00000
- #define INT_ENABLE_MASK_RAW INT_TIMING
- #define INT_ENABLE_MASK_ATOMIC (INT_TRANSACTION_DONE | \
- INT_SLAVE_EVENT | \
- INT_ADDR_ACK_ERR | \
- INT_WRITE_ACK_ERR)
- #define INT_ENABLE_MASK_AUTOMATIC (INT_SCLK_LOW_TIMEOUT | \
- INT_ADDR_ACK_ERR | \
- INT_WRITE_ACK_ERR | \
- INT_FIFO_FULL | \
- INT_FIFO_FILLING | \
- INT_FIFO_EMPTY | \
- INT_MASTER_HALTED | \
- INT_STOP_DETECTED)
- #define INT_ENABLE_MASK_WAITSTOP (INT_SLAVE_EVENT | \
- INT_ADDR_ACK_ERR | \
- INT_WRITE_ACK_ERR)
- #define LINESTAT_SCLK_LINE_STATUS BIT(0)
- #define LINESTAT_SCLK_EN BIT(1)
- #define LINESTAT_SDAT_LINE_STATUS BIT(2)
- #define LINESTAT_SDAT_EN BIT(3)
- #define LINESTAT_DET_START_STATUS BIT(4)
- #define LINESTAT_DET_STOP_STATUS BIT(5)
- #define LINESTAT_DET_ACK_STATUS BIT(6)
- #define LINESTAT_DET_NACK_STATUS BIT(7)
- #define LINESTAT_BUS_IDLE BIT(8)
- #define LINESTAT_T_DONE_STATUS BIT(9)
- #define LINESTAT_SCLK_OUT_STATUS BIT(10)
- #define LINESTAT_SDAT_OUT_STATUS BIT(11)
- #define LINESTAT_GEN_LINE_MASK_STATUS BIT(12)
- #define LINESTAT_START_BIT_DET BIT(13)
- #define LINESTAT_STOP_BIT_DET BIT(14)
- #define LINESTAT_ACK_DET BIT(15)
- #define LINESTAT_NACK_DET BIT(16)
- #define LINESTAT_INPUT_HELD_V BIT(17)
- #define LINESTAT_ABORT_DET BIT(18)
- #define LINESTAT_ACK_OR_NACK_DET (LINESTAT_ACK_DET | LINESTAT_NACK_DET)
- #define LINESTAT_INPUT_DATA 0xff000000
- #define LINESTAT_INPUT_DATA_SHIFT 24
- #define LINESTAT_CLEAR_SHIFT 13
- #define LINESTAT_LATCHED (0x3f << LINESTAT_CLEAR_SHIFT)
- #define OVERRIDE_SCLK_OVR BIT(0)
- #define OVERRIDE_SCLKEN_OVR BIT(1)
- #define OVERRIDE_SDAT_OVR BIT(2)
- #define OVERRIDE_SDATEN_OVR BIT(3)
- #define OVERRIDE_MASTER BIT(9)
- #define OVERRIDE_LINE_OVR_EN BIT(10)
- #define OVERRIDE_DIRECT BIT(11)
- #define OVERRIDE_CMD_SHIFT 4
- #define OVERRIDE_CMD_MASK 0x1f
- #define OVERRIDE_DATA_SHIFT 24
- #define OVERRIDE_SCLK_DOWN (OVERRIDE_LINE_OVR_EN | \
- OVERRIDE_SCLKEN_OVR)
- #define OVERRIDE_SCLK_UP (OVERRIDE_LINE_OVR_EN | \
- OVERRIDE_SCLKEN_OVR | \
- OVERRIDE_SCLK_OVR)
- #define OVERRIDE_SDAT_DOWN (OVERRIDE_LINE_OVR_EN | \
- OVERRIDE_SDATEN_OVR)
- #define OVERRIDE_SDAT_UP (OVERRIDE_LINE_OVR_EN | \
- OVERRIDE_SDATEN_OVR | \
- OVERRIDE_SDAT_OVR)
- #define CMD_PAUSE 0x00
- #define CMD_GEN_DATA 0x01
- #define CMD_GEN_START 0x02
- #define CMD_GEN_STOP 0x03
- #define CMD_GEN_ACK 0x04
- #define CMD_GEN_NACK 0x05
- #define CMD_RET_DATA 0x08
- #define CMD_RET_ACK 0x09
- #define TIMEOUT_TBI 0x0
- #define TIMEOUT_TSL 0xffff
- #define TIMEOUT_TDL 0x0
- #define IMG_I2C_TIMEOUT (msecs_to_jiffies(1000))
- #define SCB_OPT_INC 64
- #define SCB_FILT_GLITCH 25
- #define ISR_COMPLETE_M BIT(31)
- #define ISR_FATAL_M BIT(30)
- #define ISR_WAITSTOP BIT(29)
- #define ISR_STATUS_M 0x0000ffff
- #define ISR_COMPLETE(err) (ISR_COMPLETE_M | (ISR_STATUS_M & (err)))
- #define ISR_FATAL(err) (ISR_COMPLETE(err) | ISR_FATAL_M)
- enum img_i2c_mode {
- MODE_INACTIVE,
- MODE_RAW,
- MODE_ATOMIC,
- MODE_AUTOMATIC,
- MODE_SEQUENCE,
- MODE_FATAL,
- MODE_WAITSTOP,
- MODE_SUSPEND,
- };
- struct img_i2c_timings {
- const char *name;
- unsigned int max_bitrate;
- unsigned int tckh, tckl, tsdh, tsdl;
- unsigned int tp2s, tpl, tph;
- };
- static struct img_i2c_timings timings[] = {
-
- {
- .name = "standard",
- .max_bitrate = 100000,
- .tckh = 4000,
- .tckl = 4700,
- .tsdh = 4700,
- .tsdl = 8700,
- .tp2s = 4700,
- .tpl = 4700,
- .tph = 4000,
- },
-
- {
- .name = "fast",
- .max_bitrate = 400000,
- .tckh = 600,
- .tckl = 1300,
- .tsdh = 600,
- .tsdl = 1200,
- .tp2s = 1300,
- .tpl = 600,
- .tph = 600,
- },
- };
- static u8 img_i2c_reset_seq[] = { CMD_GEN_START,
- CMD_GEN_DATA, 0xff,
- CMD_RET_ACK,
- CMD_GEN_START,
- CMD_GEN_STOP,
- 0 };
- static u8 img_i2c_stop_seq[] = { CMD_GEN_STOP,
- 0 };
- static unsigned int img_i2c_int_enable_by_mode[] = {
- [MODE_INACTIVE] = INT_ENABLE_MASK_INACTIVE,
- [MODE_RAW] = INT_ENABLE_MASK_RAW,
- [MODE_ATOMIC] = INT_ENABLE_MASK_ATOMIC,
- [MODE_AUTOMATIC] = INT_ENABLE_MASK_AUTOMATIC,
- [MODE_SEQUENCE] = INT_ENABLE_MASK_ATOMIC,
- [MODE_FATAL] = 0,
- [MODE_WAITSTOP] = INT_ENABLE_MASK_WAITSTOP,
- [MODE_SUSPEND] = 0,
- };
- static const char * const img_i2c_atomic_cmd_names[] = {
- [CMD_PAUSE] = "PAUSE",
- [CMD_GEN_DATA] = "GEN_DATA",
- [CMD_GEN_START] = "GEN_START",
- [CMD_GEN_STOP] = "GEN_STOP",
- [CMD_GEN_ACK] = "GEN_ACK",
- [CMD_GEN_NACK] = "GEN_NACK",
- [CMD_RET_DATA] = "RET_DATA",
- [CMD_RET_ACK] = "RET_ACK",
- };
- struct img_i2c {
- struct i2c_adapter adap;
- void __iomem *base;
-
- struct clk *scb_clk, *sys_clk;
- unsigned int bitrate;
- bool need_wr_rd_fence;
-
- struct completion msg_complete;
- spinlock_t lock;
- struct i2c_msg msg;
-
- bool last_msg;
- int msg_status;
- enum img_i2c_mode mode;
- u32 int_enable;
- u32 line_status;
-
- struct timer_list check_timer;
- bool t_halt;
-
- bool at_t_done;
- bool at_slave_event;
- int at_cur_cmd;
- u8 at_cur_data;
-
- u8 *seq;
-
- unsigned int raw_timeout;
- };
- static void img_i2c_writel(struct img_i2c *i2c, u32 offset, u32 value)
- {
- writel(value, i2c->base + offset);
- }
- static u32 img_i2c_readl(struct img_i2c *i2c, u32 offset)
- {
- return readl(i2c->base + offset);
- }
- static void img_i2c_wr_rd_fence(struct img_i2c *i2c)
- {
- if (i2c->need_wr_rd_fence) {
- img_i2c_writel(i2c, SCB_CORE_REV_REG, 0);
- img_i2c_writel(i2c, SCB_CORE_REV_REG, 0);
- }
- }
- static void img_i2c_switch_mode(struct img_i2c *i2c, enum img_i2c_mode mode)
- {
- i2c->mode = mode;
- i2c->int_enable = img_i2c_int_enable_by_mode[mode];
- i2c->line_status = 0;
- }
- static void img_i2c_raw_op(struct img_i2c *i2c)
- {
- i2c->raw_timeout = 0;
- img_i2c_writel(i2c, SCB_OVERRIDE_REG,
- OVERRIDE_SCLKEN_OVR |
- OVERRIDE_SDATEN_OVR |
- OVERRIDE_MASTER |
- OVERRIDE_LINE_OVR_EN |
- OVERRIDE_DIRECT |
- ((i2c->at_cur_cmd & OVERRIDE_CMD_MASK) << OVERRIDE_CMD_SHIFT) |
- (i2c->at_cur_data << OVERRIDE_DATA_SHIFT));
- }
- static const char *img_i2c_atomic_op_name(unsigned int cmd)
- {
- if (unlikely(cmd >= ARRAY_SIZE(img_i2c_atomic_cmd_names)))
- return "UNKNOWN";
- return img_i2c_atomic_cmd_names[cmd];
- }
- static void img_i2c_atomic_op(struct img_i2c *i2c, int cmd, u8 data)
- {
- i2c->at_cur_cmd = cmd;
- i2c->at_cur_data = data;
-
- if (cmd == CMD_GEN_DATA && i2c->mode == MODE_ATOMIC) {
- u32 line_status = img_i2c_readl(i2c, SCB_STATUS_REG);
- if (line_status & LINESTAT_SDAT_LINE_STATUS && !(data & 0x80)) {
-
- img_i2c_switch_mode(i2c, MODE_RAW);
- img_i2c_raw_op(i2c);
- return;
- }
- }
- dev_dbg(i2c->adap.dev.parent,
- "atomic cmd=%s (%d) data=%#x\n",
- img_i2c_atomic_op_name(cmd), cmd, data);
- i2c->at_t_done = (cmd == CMD_RET_DATA || cmd == CMD_RET_ACK);
- i2c->at_slave_event = false;
- i2c->line_status = 0;
- img_i2c_writel(i2c, SCB_OVERRIDE_REG,
- ((cmd & OVERRIDE_CMD_MASK) << OVERRIDE_CMD_SHIFT) |
- OVERRIDE_MASTER |
- OVERRIDE_DIRECT |
- (data << OVERRIDE_DATA_SHIFT));
- }
- static void img_i2c_atomic_start(struct img_i2c *i2c)
- {
- img_i2c_switch_mode(i2c, MODE_ATOMIC);
- img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
- img_i2c_atomic_op(i2c, CMD_GEN_START, 0x00);
- }
- static void img_i2c_soft_reset(struct img_i2c *i2c)
- {
- i2c->t_halt = false;
- img_i2c_writel(i2c, SCB_CONTROL_REG, 0);
- img_i2c_writel(i2c, SCB_CONTROL_REG,
- SCB_CONTROL_CLK_ENABLE | SCB_CONTROL_SOFT_RESET);
- }
- static void img_i2c_transaction_halt(struct img_i2c *i2c, bool t_halt)
- {
- u32 val;
- if (i2c->t_halt == t_halt)
- return;
- i2c->t_halt = t_halt;
- val = img_i2c_readl(i2c, SCB_CONTROL_REG);
- if (t_halt)
- val |= SCB_CONTROL_TRANSACTION_HALT;
- else
- val &= ~SCB_CONTROL_TRANSACTION_HALT;
- img_i2c_writel(i2c, SCB_CONTROL_REG, val);
- }
- static void img_i2c_read_fifo(struct img_i2c *i2c)
- {
- while (i2c->msg.len) {
- u32 fifo_status;
- u8 data;
- img_i2c_wr_rd_fence(i2c);
- fifo_status = img_i2c_readl(i2c, SCB_FIFO_STATUS_REG);
- if (fifo_status & FIFO_READ_EMPTY)
- break;
- data = img_i2c_readl(i2c, SCB_READ_DATA_REG);
- *i2c->msg.buf = data;
- img_i2c_writel(i2c, SCB_READ_FIFO_REG, 0xff);
- i2c->msg.len--;
- i2c->msg.buf++;
- }
- }
- static void img_i2c_write_fifo(struct img_i2c *i2c)
- {
- while (i2c->msg.len) {
- u32 fifo_status;
- img_i2c_wr_rd_fence(i2c);
- fifo_status = img_i2c_readl(i2c, SCB_FIFO_STATUS_REG);
- if (fifo_status & FIFO_WRITE_FULL)
- break;
- img_i2c_writel(i2c, SCB_WRITE_DATA_REG, *i2c->msg.buf);
- i2c->msg.len--;
- i2c->msg.buf++;
- }
-
- if (!i2c->msg.len)
- i2c->int_enable &= ~INT_FIFO_EMPTYING;
- }
- static void img_i2c_read(struct img_i2c *i2c)
- {
- img_i2c_switch_mode(i2c, MODE_AUTOMATIC);
- if (!i2c->last_msg)
- i2c->int_enable |= INT_SLAVE_EVENT;
- img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
- img_i2c_writel(i2c, SCB_READ_ADDR_REG, i2c->msg.addr);
- img_i2c_writel(i2c, SCB_READ_COUNT_REG, i2c->msg.len);
- mod_timer(&i2c->check_timer, jiffies + msecs_to_jiffies(1));
- }
- static void img_i2c_write(struct img_i2c *i2c)
- {
- img_i2c_switch_mode(i2c, MODE_AUTOMATIC);
- if (!i2c->last_msg)
- i2c->int_enable |= INT_SLAVE_EVENT;
- img_i2c_writel(i2c, SCB_WRITE_ADDR_REG, i2c->msg.addr);
- img_i2c_writel(i2c, SCB_WRITE_COUNT_REG, i2c->msg.len);
- mod_timer(&i2c->check_timer, jiffies + msecs_to_jiffies(1));
- img_i2c_write_fifo(i2c);
-
- img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
- }
- static void img_i2c_complete_transaction(struct img_i2c *i2c, int status)
- {
- img_i2c_switch_mode(i2c, MODE_INACTIVE);
- if (status) {
- i2c->msg_status = status;
- img_i2c_transaction_halt(i2c, false);
- }
- complete(&i2c->msg_complete);
- }
- static unsigned int img_i2c_raw_atomic_delay_handler(struct img_i2c *i2c,
- u32 int_status, u32 line_status)
- {
-
- img_i2c_atomic_op(i2c, i2c->at_cur_cmd, i2c->at_cur_data);
- img_i2c_switch_mode(i2c, MODE_ATOMIC);
- return 0;
- }
- static unsigned int img_i2c_raw(struct img_i2c *i2c, u32 int_status,
- u32 line_status)
- {
- if (int_status & INT_TIMING) {
- if (i2c->raw_timeout == 0)
- return img_i2c_raw_atomic_delay_handler(i2c,
- int_status, line_status);
- --i2c->raw_timeout;
- }
- return 0;
- }
- static unsigned int img_i2c_sequence(struct img_i2c *i2c, u32 int_status)
- {
- static const unsigned int continue_bits[] = {
- [CMD_GEN_START] = LINESTAT_START_BIT_DET,
- [CMD_GEN_DATA] = LINESTAT_INPUT_HELD_V,
- [CMD_RET_ACK] = LINESTAT_ACK_DET | LINESTAT_NACK_DET,
- [CMD_RET_DATA] = LINESTAT_INPUT_HELD_V,
- [CMD_GEN_STOP] = LINESTAT_STOP_BIT_DET,
- };
- int next_cmd = -1;
- u8 next_data = 0x00;
- if (int_status & INT_SLAVE_EVENT)
- i2c->at_slave_event = true;
- if (int_status & INT_TRANSACTION_DONE)
- i2c->at_t_done = true;
- if (!i2c->at_slave_event || !i2c->at_t_done)
- return 0;
-
- if (i2c->at_cur_cmd >= 0 &&
- i2c->at_cur_cmd < ARRAY_SIZE(continue_bits)) {
- unsigned int cont_bits = continue_bits[i2c->at_cur_cmd];
- if (cont_bits) {
- cont_bits |= LINESTAT_ABORT_DET;
- if (!(i2c->line_status & cont_bits))
- return 0;
- }
- }
-
- next_cmd = *i2c->seq;
-
- if (!next_cmd) {
- img_i2c_writel(i2c, SCB_OVERRIDE_REG, 0);
- return ISR_COMPLETE(0);
- }
-
- if (next_cmd == CMD_GEN_DATA) {
- ++i2c->seq;
- next_data = *i2c->seq;
- }
- ++i2c->seq;
- img_i2c_atomic_op(i2c, next_cmd, next_data);
- return 0;
- }
- static void img_i2c_reset_start(struct img_i2c *i2c)
- {
-
- img_i2c_switch_mode(i2c, MODE_SEQUENCE);
- img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
- i2c->seq = img_i2c_reset_seq;
- i2c->at_slave_event = true;
- i2c->at_t_done = true;
- i2c->at_cur_cmd = -1;
-
- img_i2c_sequence(i2c, 0);
- }
- static void img_i2c_stop_start(struct img_i2c *i2c)
- {
-
- img_i2c_switch_mode(i2c, MODE_SEQUENCE);
- img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
- i2c->seq = img_i2c_stop_seq;
- i2c->at_slave_event = true;
- i2c->at_t_done = true;
- i2c->at_cur_cmd = -1;
-
- img_i2c_sequence(i2c, 0);
- }
- static unsigned int img_i2c_atomic(struct img_i2c *i2c,
- u32 int_status,
- u32 line_status)
- {
- int next_cmd = -1;
- u8 next_data = 0x00;
- if (int_status & INT_SLAVE_EVENT)
- i2c->at_slave_event = true;
- if (int_status & INT_TRANSACTION_DONE)
- i2c->at_t_done = true;
- if (!i2c->at_slave_event || !i2c->at_t_done)
- goto next_atomic_cmd;
- if (i2c->line_status & LINESTAT_ABORT_DET) {
- dev_dbg(i2c->adap.dev.parent, "abort condition detected\n");
- next_cmd = CMD_GEN_STOP;
- i2c->msg_status = -EIO;
- goto next_atomic_cmd;
- }
-
- switch (i2c->at_cur_cmd) {
- case CMD_GEN_START:
- next_cmd = CMD_GEN_DATA;
- next_data = i2c_8bit_addr_from_msg(&i2c->msg);
- break;
- case CMD_GEN_DATA:
- if (i2c->line_status & LINESTAT_INPUT_HELD_V)
- next_cmd = CMD_RET_ACK;
- break;
- case CMD_RET_ACK:
- if (i2c->line_status & LINESTAT_ACK_DET ||
- (i2c->line_status & LINESTAT_NACK_DET &&
- i2c->msg.flags & I2C_M_IGNORE_NAK)) {
- if (i2c->msg.len == 0) {
- next_cmd = CMD_GEN_STOP;
- } else if (i2c->msg.flags & I2C_M_RD) {
- next_cmd = CMD_RET_DATA;
- } else {
- next_cmd = CMD_GEN_DATA;
- next_data = *i2c->msg.buf;
- --i2c->msg.len;
- ++i2c->msg.buf;
- }
- } else if (i2c->line_status & LINESTAT_NACK_DET) {
- i2c->msg_status = -EIO;
- next_cmd = CMD_GEN_STOP;
- }
- break;
- case CMD_RET_DATA:
- if (i2c->line_status & LINESTAT_INPUT_HELD_V) {
- *i2c->msg.buf = (i2c->line_status &
- LINESTAT_INPUT_DATA)
- >> LINESTAT_INPUT_DATA_SHIFT;
- --i2c->msg.len;
- ++i2c->msg.buf;
- if (i2c->msg.len)
- next_cmd = CMD_GEN_ACK;
- else
- next_cmd = CMD_GEN_NACK;
- }
- break;
- case CMD_GEN_ACK:
- if (i2c->line_status & LINESTAT_ACK_DET) {
- next_cmd = CMD_RET_DATA;
- } else {
- i2c->msg_status = -EIO;
- next_cmd = CMD_GEN_STOP;
- }
- break;
- case CMD_GEN_NACK:
- next_cmd = CMD_GEN_STOP;
- break;
- case CMD_GEN_STOP:
- img_i2c_writel(i2c, SCB_OVERRIDE_REG, 0);
- return ISR_COMPLETE(0);
- default:
- dev_err(i2c->adap.dev.parent, "bad atomic command %d\n",
- i2c->at_cur_cmd);
- i2c->msg_status = -EIO;
- next_cmd = CMD_GEN_STOP;
- break;
- }
- next_atomic_cmd:
- if (next_cmd != -1) {
-
- if (next_cmd == CMD_GEN_STOP && !i2c->msg_status &&
- !i2c->last_msg)
- return ISR_COMPLETE(0);
- img_i2c_atomic_op(i2c, next_cmd, next_data);
- }
- return 0;
- }
- static void img_i2c_check_timer(unsigned long arg)
- {
- struct img_i2c *i2c = (struct img_i2c *)arg;
- unsigned long flags;
- unsigned int line_status;
- spin_lock_irqsave(&i2c->lock, flags);
- line_status = img_i2c_readl(i2c, SCB_STATUS_REG);
-
- if (line_status & LINESTAT_ABORT_DET) {
- dev_dbg(i2c->adap.dev.parent,
- "abort condition detected by check timer\n");
-
- img_i2c_writel(i2c, SCB_INT_MASK_REG,
- i2c->int_enable | INT_SLAVE_EVENT);
- }
- spin_unlock_irqrestore(&i2c->lock, flags);
- }
- static unsigned int img_i2c_auto(struct img_i2c *i2c,
- unsigned int int_status,
- unsigned int line_status)
- {
- if (int_status & (INT_WRITE_ACK_ERR | INT_ADDR_ACK_ERR))
- return ISR_COMPLETE(EIO);
- if (line_status & LINESTAT_ABORT_DET) {
- dev_dbg(i2c->adap.dev.parent, "abort condition detected\n");
-
- if ((i2c->msg.flags & I2C_M_RD) &&
- (int_status & INT_FIFO_FULL_FILLING))
- img_i2c_read_fifo(i2c);
-
- i2c->msg_status = -EIO;
- img_i2c_stop_start(i2c);
- return 0;
- }
-
- if (!i2c->last_msg && line_status & LINESTAT_START_BIT_DET) {
- img_i2c_transaction_halt(i2c, !i2c->last_msg);
-
- i2c->int_enable &= ~INT_SLAVE_EVENT;
- }
- mod_timer(&i2c->check_timer, jiffies + msecs_to_jiffies(1));
- if (int_status & INT_STOP_DETECTED) {
-
- if (i2c->msg.flags & I2C_M_RD)
- img_i2c_read_fifo(i2c);
- return ISR_COMPLETE(0);
- }
- if (i2c->msg.flags & I2C_M_RD) {
- if (int_status & (INT_FIFO_FULL_FILLING | INT_MASTER_HALTED)) {
- img_i2c_read_fifo(i2c);
- if (i2c->msg.len == 0)
- return ISR_WAITSTOP;
- }
- } else {
- if (int_status & (INT_FIFO_EMPTY | INT_MASTER_HALTED)) {
- if ((int_status & INT_FIFO_EMPTY) &&
- i2c->msg.len == 0)
- return ISR_WAITSTOP;
- img_i2c_write_fifo(i2c);
- }
- }
- if (int_status & INT_MASTER_HALTED) {
-
- img_i2c_transaction_halt(i2c, false);
- img_i2c_transaction_halt(i2c, !i2c->last_msg);
- }
- return 0;
- }
- static irqreturn_t img_i2c_isr(int irq, void *dev_id)
- {
- struct img_i2c *i2c = (struct img_i2c *)dev_id;
- u32 int_status, line_status;
-
- unsigned int hret;
-
- int_status = img_i2c_readl(i2c, SCB_INT_STATUS_REG);
-
- img_i2c_writel(i2c, SCB_INT_CLEAR_REG, int_status);
-
- line_status = img_i2c_readl(i2c, SCB_STATUS_REG);
- if (line_status & LINESTAT_LATCHED) {
- img_i2c_writel(i2c, SCB_CLEAR_REG,
- (line_status & LINESTAT_LATCHED)
- >> LINESTAT_CLEAR_SHIFT);
- img_i2c_wr_rd_fence(i2c);
- }
- spin_lock(&i2c->lock);
-
- i2c->line_status &= ~LINESTAT_INPUT_DATA;
- i2c->line_status |= line_status;
-
- if ((int_status & INT_SCLK_LOW_TIMEOUT) &&
- !(int_status & (INT_SLAVE_EVENT |
- INT_FIFO_EMPTY |
- INT_FIFO_FULL))) {
- dev_crit(i2c->adap.dev.parent,
- "fatal: clock low timeout occurred %s addr 0x%02x\n",
- (i2c->msg.flags & I2C_M_RD) ? "reading" : "writing",
- i2c->msg.addr);
- hret = ISR_FATAL(EIO);
- goto out;
- }
- if (i2c->mode == MODE_ATOMIC)
- hret = img_i2c_atomic(i2c, int_status, line_status);
- else if (i2c->mode == MODE_AUTOMATIC)
- hret = img_i2c_auto(i2c, int_status, line_status);
- else if (i2c->mode == MODE_SEQUENCE)
- hret = img_i2c_sequence(i2c, int_status);
- else if (i2c->mode == MODE_WAITSTOP && (int_status & INT_SLAVE_EVENT) &&
- (line_status & LINESTAT_STOP_BIT_DET))
- hret = ISR_COMPLETE(0);
- else if (i2c->mode == MODE_RAW)
- hret = img_i2c_raw(i2c, int_status, line_status);
- else
- hret = 0;
-
- img_i2c_writel(i2c, SCB_INT_CLEAR_REG, int_status & INT_LEVEL);
- out:
- if (hret & ISR_WAITSTOP) {
-
- if (!i2c->last_msg || i2c->line_status & LINESTAT_STOP_BIT_DET)
- hret = ISR_COMPLETE(0);
- else
- img_i2c_switch_mode(i2c, MODE_WAITSTOP);
- }
-
- if (hret & ISR_COMPLETE_M) {
- int status = -(hret & ISR_STATUS_M);
- img_i2c_complete_transaction(i2c, status);
- if (hret & ISR_FATAL_M)
- img_i2c_switch_mode(i2c, MODE_FATAL);
- }
-
- img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
- spin_unlock(&i2c->lock);
- return IRQ_HANDLED;
- }
- static int img_i2c_reset_bus(struct img_i2c *i2c)
- {
- unsigned long flags;
- unsigned long time_left;
- spin_lock_irqsave(&i2c->lock, flags);
- reinit_completion(&i2c->msg_complete);
- img_i2c_reset_start(i2c);
- spin_unlock_irqrestore(&i2c->lock, flags);
- time_left = wait_for_completion_timeout(&i2c->msg_complete,
- IMG_I2C_TIMEOUT);
- if (time_left == 0)
- return -ETIMEDOUT;
- return 0;
- }
- static int img_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
- int num)
- {
- struct img_i2c *i2c = i2c_get_adapdata(adap);
- bool atomic = false;
- int i, ret;
- unsigned long time_left;
- if (i2c->mode == MODE_SUSPEND) {
- WARN(1, "refusing to service transaction in suspended state\n");
- return -EIO;
- }
- if (i2c->mode == MODE_FATAL)
- return -EIO;
- for (i = 0; i < num; i++) {
-
- if (!msgs[i].len && msgs[i].flags & I2C_M_RD)
- return -EIO;
-
- if (!msgs[i].len ||
- (msgs[i].flags & I2C_M_IGNORE_NAK))
- atomic = true;
- }
- ret = clk_prepare_enable(i2c->scb_clk);
- if (ret)
- return ret;
- for (i = 0; i < num; i++) {
- struct i2c_msg *msg = &msgs[i];
- unsigned long flags;
- spin_lock_irqsave(&i2c->lock, flags);
-
- i2c->msg = *msg;
- i2c->msg_status = 0;
-
- i2c->last_msg = (i == num - 1);
- reinit_completion(&i2c->msg_complete);
-
- img_i2c_writel(i2c, SCB_INT_CLEAR_REG, ~0);
- img_i2c_writel(i2c, SCB_CLEAR_REG, ~0);
- if (atomic) {
- img_i2c_atomic_start(i2c);
- } else {
-
- img_i2c_transaction_halt(i2c, !i2c->last_msg);
- if (msg->flags & I2C_M_RD)
- img_i2c_read(i2c);
- else
- img_i2c_write(i2c);
-
- img_i2c_transaction_halt(i2c, false);
- img_i2c_transaction_halt(i2c, !i2c->last_msg);
- }
- spin_unlock_irqrestore(&i2c->lock, flags);
- time_left = wait_for_completion_timeout(&i2c->msg_complete,
- IMG_I2C_TIMEOUT);
- del_timer_sync(&i2c->check_timer);
- if (time_left == 0) {
- dev_err(adap->dev.parent, "i2c transfer timed out\n");
- i2c->msg_status = -ETIMEDOUT;
- break;
- }
- if (i2c->msg_status)
- break;
- }
- clk_disable_unprepare(i2c->scb_clk);
- return i2c->msg_status ? i2c->msg_status : num;
- }
- static u32 img_i2c_func(struct i2c_adapter *adap)
- {
- return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
- }
- static const struct i2c_algorithm img_i2c_algo = {
- .master_xfer = img_i2c_xfer,
- .functionality = img_i2c_func,
- };
- static int img_i2c_init(struct img_i2c *i2c)
- {
- unsigned int clk_khz, bitrate_khz, clk_period, tckh, tckl, tsdh;
- unsigned int i, ret, data, prescale, inc, int_bitrate, filt;
- struct img_i2c_timings timing;
- u32 rev;
- ret = clk_prepare_enable(i2c->scb_clk);
- if (ret)
- return ret;
- rev = img_i2c_readl(i2c, SCB_CORE_REV_REG);
- if ((rev & 0x00ffffff) < 0x00020200) {
- dev_info(i2c->adap.dev.parent,
- "Unknown hardware revision (%d.%d.%d.%d)\n",
- (rev >> 24) & 0xff, (rev >> 16) & 0xff,
- (rev >> 8) & 0xff, rev & 0xff);
- clk_disable_unprepare(i2c->scb_clk);
- return -EINVAL;
- }
-
- i2c->need_wr_rd_fence = true;
-
- timing = timings[0];
- for (i = 0; i < ARRAY_SIZE(timings); i++) {
- if (i2c->bitrate <= timings[i].max_bitrate) {
- timing = timings[i];
- break;
- }
- }
- if (i2c->bitrate > timings[ARRAY_SIZE(timings) - 1].max_bitrate) {
- dev_warn(i2c->adap.dev.parent,
- "requested bitrate (%u) is higher than the max bitrate supported (%u)\n",
- i2c->bitrate,
- timings[ARRAY_SIZE(timings) - 1].max_bitrate);
- timing = timings[ARRAY_SIZE(timings) - 1];
- i2c->bitrate = timing.max_bitrate;
- }
- bitrate_khz = i2c->bitrate / 1000;
- clk_khz = clk_get_rate(i2c->scb_clk) / 1000;
-
- prescale = SCB_OPT_INC * clk_khz / (256 * 16 * bitrate_khz);
- prescale = clamp_t(unsigned int, prescale, 1, 8);
- clk_khz /= prescale;
-
- inc = (256 * 16 * bitrate_khz) / clk_khz;
-
- if (clk_khz < 20000) {
- filt = SCB_FILT_DISABLE;
- } else if (clk_khz < 40000) {
- filt = SCB_FILT_BYPASS;
- } else {
-
- filt = (64000 / ((clk_khz / 1000) * SCB_FILT_GLITCH));
-
- if (64000 % ((clk_khz / 1000) * SCB_FILT_GLITCH))
- inc++;
- if (filt > SCB_FILT_INC_MASK)
- filt = SCB_FILT_INC_MASK;
- filt = (filt & SCB_FILT_INC_MASK) << SCB_FILT_INC_SHIFT;
- }
- data = filt | ((inc & SCB_INC_MASK) << SCB_INC_SHIFT) | (prescale - 1);
- img_i2c_writel(i2c, SCB_CLK_SET_REG, data);
-
- clk_period = (256 * 1000000) / (clk_khz * inc);
-
- int_bitrate = 1000000 / (bitrate_khz * clk_period);
- if ((1000000 % (bitrate_khz * clk_period)) >=
- ((bitrate_khz * clk_period) / 2))
- int_bitrate++;
-
- tckh = int_bitrate / 2;
- tckl = int_bitrate - tckh;
-
- data = DIV_ROUND_UP(timing.tckl, clk_period);
- if (tckl < data) {
- tckl = data;
- tckh = int_bitrate - tckl;
- }
- if (tckh > 0)
- --tckh;
- if (tckl > 0)
- --tckl;
- img_i2c_writel(i2c, SCB_TIME_TCKH_REG, tckh);
- img_i2c_writel(i2c, SCB_TIME_TCKL_REG, tckl);
-
- tsdh = DIV_ROUND_UP(timing.tsdh, clk_period);
- if (tsdh > 1)
- data = tsdh - 1;
- else
- data = 0x01;
- img_i2c_writel(i2c, SCB_TIME_TSDH_REG, data);
-
- tsdh = data;
-
- data = timing.tpl / clk_period;
- if (data > 0)
- --data;
- img_i2c_writel(i2c, SCB_TIME_TPL_REG, data);
-
- data = timing.tph / clk_period;
- if (data > 0)
- --data;
- img_i2c_writel(i2c, SCB_TIME_TPH_REG, data);
-
- img_i2c_writel(i2c, SCB_TIME_TSDL_REG, data + tsdh + 2);
-
- data = timing.tp2s / clk_period;
- if (data > 0)
- --data;
- img_i2c_writel(i2c, SCB_TIME_TP2S_REG, data);
- img_i2c_writel(i2c, SCB_TIME_TBI_REG, TIMEOUT_TBI);
- img_i2c_writel(i2c, SCB_TIME_TSL_REG, TIMEOUT_TSL);
- img_i2c_writel(i2c, SCB_TIME_TDL_REG, TIMEOUT_TDL);
-
- img_i2c_soft_reset(i2c);
-
- img_i2c_writel(i2c, SCB_INT_MASK_REG, 0);
-
- img_i2c_writel(i2c, SCB_INT_CLEAR_REG, ~0);
-
- img_i2c_writel(i2c, SCB_CLEAR_REG, ~0);
-
- img_i2c_writel(i2c, SCB_INT_MASK_REG, i2c->int_enable);
-
- ret = img_i2c_reset_bus(i2c);
- clk_disable_unprepare(i2c->scb_clk);
- return ret;
- }
- static int img_i2c_probe(struct platform_device *pdev)
- {
- struct device_node *node = pdev->dev.of_node;
- struct img_i2c *i2c;
- struct resource *res;
- int irq, ret;
- u32 val;
- i2c = devm_kzalloc(&pdev->dev, sizeof(struct img_i2c), GFP_KERNEL);
- if (!i2c)
- return -ENOMEM;
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- i2c->base = devm_ioremap_resource(&pdev->dev, res);
- if (IS_ERR(i2c->base))
- return PTR_ERR(i2c->base);
- irq = platform_get_irq(pdev, 0);
- if (irq < 0) {
- dev_err(&pdev->dev, "can't get irq number\n");
- return irq;
- }
- i2c->sys_clk = devm_clk_get(&pdev->dev, "sys");
- if (IS_ERR(i2c->sys_clk)) {
- dev_err(&pdev->dev, "can't get system clock\n");
- return PTR_ERR(i2c->sys_clk);
- }
- i2c->scb_clk = devm_clk_get(&pdev->dev, "scb");
- if (IS_ERR(i2c->scb_clk)) {
- dev_err(&pdev->dev, "can't get core clock\n");
- return PTR_ERR(i2c->scb_clk);
- }
- ret = devm_request_irq(&pdev->dev, irq, img_i2c_isr, 0,
- pdev->name, i2c);
- if (ret) {
- dev_err(&pdev->dev, "can't request irq %d\n", irq);
- return ret;
- }
-
- init_timer(&i2c->check_timer);
- i2c->check_timer.function = img_i2c_check_timer;
- i2c->check_timer.data = (unsigned long)i2c;
- i2c->bitrate = timings[0].max_bitrate;
- if (!of_property_read_u32(node, "clock-frequency", &val))
- i2c->bitrate = val;
- i2c_set_adapdata(&i2c->adap, i2c);
- i2c->adap.dev.parent = &pdev->dev;
- i2c->adap.dev.of_node = node;
- i2c->adap.owner = THIS_MODULE;
- i2c->adap.algo = &img_i2c_algo;
- i2c->adap.retries = 5;
- i2c->adap.nr = pdev->id;
- snprintf(i2c->adap.name, sizeof(i2c->adap.name), "IMG SCB I2C");
- img_i2c_switch_mode(i2c, MODE_INACTIVE);
- spin_lock_init(&i2c->lock);
- init_completion(&i2c->msg_complete);
- platform_set_drvdata(pdev, i2c);
- ret = clk_prepare_enable(i2c->sys_clk);
- if (ret)
- return ret;
- ret = img_i2c_init(i2c);
- if (ret)
- goto disable_clk;
- ret = i2c_add_numbered_adapter(&i2c->adap);
- if (ret < 0)
- goto disable_clk;
- return 0;
- disable_clk:
- clk_disable_unprepare(i2c->sys_clk);
- return ret;
- }
- static int img_i2c_remove(struct platform_device *dev)
- {
- struct img_i2c *i2c = platform_get_drvdata(dev);
- i2c_del_adapter(&i2c->adap);
- clk_disable_unprepare(i2c->sys_clk);
- return 0;
- }
- #ifdef CONFIG_PM_SLEEP
- static int img_i2c_suspend(struct device *dev)
- {
- struct img_i2c *i2c = dev_get_drvdata(dev);
- img_i2c_switch_mode(i2c, MODE_SUSPEND);
- clk_disable_unprepare(i2c->sys_clk);
- return 0;
- }
- static int img_i2c_resume(struct device *dev)
- {
- struct img_i2c *i2c = dev_get_drvdata(dev);
- int ret;
- ret = clk_prepare_enable(i2c->sys_clk);
- if (ret)
- return ret;
- img_i2c_init(i2c);
- return 0;
- }
- #endif
- static SIMPLE_DEV_PM_OPS(img_i2c_pm, img_i2c_suspend, img_i2c_resume);
- static const struct of_device_id img_scb_i2c_match[] = {
- { .compatible = "img,scb-i2c" },
- { }
- };
- MODULE_DEVICE_TABLE(of, img_scb_i2c_match);
- static struct platform_driver img_scb_i2c_driver = {
- .driver = {
- .name = "img-i2c-scb",
- .of_match_table = img_scb_i2c_match,
- .pm = &img_i2c_pm,
- },
- .probe = img_i2c_probe,
- .remove = img_i2c_remove,
- };
- module_platform_driver(img_scb_i2c_driver);
- MODULE_AUTHOR("James Hogan <james.hogan@imgtec.com>");
- MODULE_DESCRIPTION("IMG host I2C driver");
- MODULE_LICENSE("GPL v2");
|