Lines Matching refs:pd
169 static void iic_wr(struct sh_mobile_i2c_data *pd, int offs, unsigned char data)
172 data |= pd->icic;
174 iowrite8(data, pd->reg + offs);
177 static unsigned char iic_rd(struct sh_mobile_i2c_data *pd, int offs)
179 return ioread8(pd->reg + offs);
182 static void iic_set_clr(struct sh_mobile_i2c_data *pd, int offs,
185 iic_wr(pd, offs, (iic_rd(pd, offs) | set) & ~clr);
188 static void activate_ch(struct sh_mobile_i2c_data *pd)
196 pm_runtime_get_sync(pd->dev);
197 clk_enable(pd->clk);
200 i2c_clk = clk_get_rate(pd->clk);
211 pd->iccl = (u_int8_t)((num/denom) + 1);
213 pd->iccl = (u_int8_t)(num/denom);
216 if (pd->flags & IIC_FLAG_HAS_ICIC67) {
218 pd->icic |= ICIC_ICCLB8;
220 pd->icic &= ~ICIC_ICCLB8;
228 pd->icch = (u_int8_t)((num/denom) + 1);
230 pd->icch = (u_int8_t)(num/denom);
233 if (pd->flags & IIC_FLAG_HAS_ICIC67) {
235 pd->icic |= ICIC_ICCHB8;
237 pd->icic &= ~ICIC_ICCHB8;
241 iic_set_clr(pd, ICCR, ICCR_ICE, 0);
244 iic_wr(pd, ICIC, 0);
247 iic_wr(pd, ICCL, pd->iccl);
248 iic_wr(pd, ICCH, pd->icch);
251 static void deactivate_ch(struct sh_mobile_i2c_data *pd)
254 iic_wr(pd, ICSR, 0);
255 iic_wr(pd, ICIC, 0);
258 iic_set_clr(pd, ICCR, 0, ICCR_ICE);
261 clk_disable(pd->clk);
262 pm_runtime_put_sync(pd->dev);
265 static unsigned char i2c_op(struct sh_mobile_i2c_data *pd,
271 dev_dbg(pd->dev, "op %d, data in 0x%02x\n", op, data);
273 spin_lock_irqsave(&pd->lock, flags);
277 iic_wr(pd, ICCR, 0x94);
280 iic_wr(pd, ICIC, ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
281 iic_wr(pd, ICDR, data);
284 iic_wr(pd, ICDR, data);
287 iic_wr(pd, ICDR, data);
288 iic_wr(pd, ICCR, 0x90);
291 iic_wr(pd, ICCR, 0x81);
294 ret = iic_rd(pd, ICDR);
297 iic_wr(pd, ICIC,
299 iic_wr(pd, ICCR, 0xc0);
302 iic_wr(pd, ICIC,
304 ret = iic_rd(pd, ICDR);
305 iic_wr(pd, ICCR, 0xc0);
309 spin_unlock_irqrestore(&pd->lock, flags);
311 dev_dbg(pd->dev, "op %d, data out 0x%02x\n", op, ret);
315 static int sh_mobile_i2c_is_first_byte(struct sh_mobile_i2c_data *pd)
317 if (pd->pos == -1)
323 static int sh_mobile_i2c_is_last_byte(struct sh_mobile_i2c_data *pd)
325 if (pd->pos == (pd->msg->len - 1))
331 static void sh_mobile_i2c_get_data(struct sh_mobile_i2c_data *pd,
334 switch (pd->pos) {
336 *buf = (pd->msg->addr & 0x7f) << 1;
337 *buf |= (pd->msg->flags & I2C_M_RD) ? 1 : 0;
340 *buf = pd->msg->buf[pd->pos];
344 static int sh_mobile_i2c_isr_tx(struct sh_mobile_i2c_data *pd)
348 if (pd->pos == pd->msg->len)
351 sh_mobile_i2c_get_data(pd, &data);
353 if (sh_mobile_i2c_is_last_byte(pd))
354 i2c_op(pd, OP_TX_STOP, data);
355 else if (sh_mobile_i2c_is_first_byte(pd))
356 i2c_op(pd, OP_TX_FIRST, data);
358 i2c_op(pd, OP_TX, data);
360 pd->pos++;
364 static int sh_mobile_i2c_isr_rx(struct sh_mobile_i2c_data *pd)
370 if (pd->pos <= -1) {
371 sh_mobile_i2c_get_data(pd, &data);
373 if (sh_mobile_i2c_is_first_byte(pd))
374 i2c_op(pd, OP_TX_FIRST, data);
376 i2c_op(pd, OP_TX, data);
380 if (pd->pos == 0) {
381 i2c_op(pd, OP_TX_TO_RX, 0);
385 real_pos = pd->pos - 2;
387 if (pd->pos == pd->msg->len) {
389 i2c_op(pd, OP_RX_STOP, 0);
392 data = i2c_op(pd, OP_RX_STOP_DATA, 0);
394 data = i2c_op(pd, OP_RX, 0);
397 pd->msg->buf[real_pos] = data;
400 pd->pos++;
401 return pd->pos == (pd->msg->len + 2);
407 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
411 sr = iic_rd(pd, ICSR);
412 pd->sr |= sr; /* remember state */
414 dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr,
415 (pd->msg->flags & I2C_M_RD) ? "read" : "write",
416 pd->pos, pd->msg->len);
420 iic_wr(pd, ICSR, sr & ~(ICSR_AL | ICSR_TACK));
422 } else if (pd->msg->flags & I2C_M_RD)
423 wakeup = sh_mobile_i2c_isr_rx(pd);
425 wakeup = sh_mobile_i2c_isr_tx(pd);
428 iic_wr(pd, ICSR, sr & ~ICSR_WAIT);
431 pd->sr |= SW_DONE;
432 wake_up(&pd->wait);
438 static int start_ch(struct sh_mobile_i2c_data *pd, struct i2c_msg *usr_msg)
441 dev_err(pd->dev, "Unsupported zero length i2c read\n");
446 iic_set_clr(pd, ICCR, 0, ICCR_ICE);
449 iic_set_clr(pd, ICCR, ICCR_ICE, 0);
452 iic_wr(pd, ICCL, pd->iccl);
453 iic_wr(pd, ICCH, pd->icch);
455 pd->msg = usr_msg;
456 pd->pos = -1;
457 pd->sr = 0;
460 iic_wr(pd, ICIC, ICIC_DTEE | ICIC_WAITE | ICIC_ALE | ICIC_TACKE);
468 struct sh_mobile_i2c_data *pd = i2c_get_adapdata(adapter);
474 activate_ch(pd);
480 err = start_ch(pd, msg);
484 i2c_op(pd, OP_START, 0);
487 k = wait_event_timeout(pd->wait,
488 pd->sr & (ICSR_TACK | SW_DONE),
491 dev_err(pd->dev, "Transfer request timed out\n");
495 val = iic_rd(pd, ICSR);
497 dev_dbg(pd->dev, "val 0x%02x pd->sr 0x%02x\n", val, pd->sr);
509 dev_err(pd->dev, "Polling timed out\n");
514 if ((val | pd->sr) & (ICSR_TACK | ICSR_AL)) {
520 deactivate_ch(pd);
574 struct sh_mobile_i2c_data *pd;
581 pd = kzalloc(sizeof(struct sh_mobile_i2c_data), GFP_KERNEL);
582 if (pd == NULL) {
588 pd->clk = clk_get(&dev->dev, clk_name);
589 if (IS_ERR(pd->clk)) {
591 ret = PTR_ERR(pd->clk);
601 pd->dev = &dev->dev;
602 platform_set_drvdata(dev, pd);
613 pd->reg = ioremap(res->start, size);
614 if (pd->reg == NULL) {
624 pd->flags |= IIC_FLAG_HAS_ICIC67;
640 adap = &pd->adap;
641 i2c_set_adapdata(adap, pd);
651 spin_lock_init(&pd->lock);
652 init_waitqueue_head(&pd->wait);
663 iounmap(pd->reg);
667 clk_put(pd->clk);
669 kfree(pd);
675 struct sh_mobile_i2c_data *pd = platform_get_drvdata(dev);
677 i2c_del_adapter(&pd->adap);
678 iounmap(pd->reg);
680 clk_put(pd->clk);
682 kfree(pd);