aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/mediatek/power/mt6735/max77819_pmic.c
blob: 0dfb2a150047876e8090abe78b3963d331d2931c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/* drivers/i2c/chips/max77819_pmic.c - MAX77819_PMIC motion sensor driver
 *
 *
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/kobject.h>
#include <linux/platform_device.h>
#include <asm/atomic.h>

#include <cust_acc.h>
#include <linux/hwmsensor.h>
#include <linux/hwmsen_dev.h>
#include <linux/sensors_io.h>
#include <linux/hwmsen_helper.h>
#include <linux/xlog.h>


#include <mach/mt_typedefs.h>
#include <mach/mt_gpio.h>
#include <mach/mt_pm_ldo.h>

#include "cust_charging.h"
#include <mach/charging.h>


#include "max77819_pmic.h"


/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
#define DEBUG 0
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
#define C_I2C_FIFO_SIZE         8
#define MAX77819_PMIC_I2C_NUM 3

#define MAX77819_PMIC_DEV_NAME        "MAX77819_PMIC"
/*----------------------------------------------------------------------------*/
static const struct i2c_device_id max77819_pmic_i2c_id[] = {{MAX77819_PMIC_DEV_NAME, 0}, {} };
/*the adapter id will be available in customization*/

static struct i2c_board_info i2c_max77819_pmic __initdata = { I2C_BOARD_INFO("MAX77819_PMIC", 0xCC>>1)};

kal_bool chargin_hw_init_done = KAL_FALSE;


/*----------------------------------------------------------------------------*/
static int max77819_pmic_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
static int max77819_pmic_i2c_remove(struct i2c_client *client);
static int max77819_pmic_suspend(struct i2c_client *client, pm_message_t msg);
static int max77819_pmic_resume(struct i2c_client *client);

/*----------------------------------------------------------------------------*/


/*------------------------------------------------------------------------------*/
typedef enum {
    BLS_TRC_READ  = 0x01,
    BLS_TRC_RAWDATA = 0x02,
    BLS_TRC_IOCTL   = 0x04,
    BLS_TRC_FILTER = 0x08,
} BLS_TRC;
/*----------------------------------------------------------------------------*/

struct max77819_pmic_i2c_data {
    struct i2c_client *client;

    /*misc*/
    atomic_t                trace;
    atomic_t                suspend;
    /*data*/
};
/*----------------------------------------------------------------------------*/
static struct i2c_driver max77819_pmic_i2c_driver = {
    .driver = {
	.name           = MAX77819_PMIC_DEV_NAME,
    },
	.probe		= max77819_pmic_i2c_probe,
	.remove		= max77819_pmic_i2c_remove,
	.suspend	= max77819_pmic_suspend,
	.resume		= max77819_pmic_resume,
	.id_table	= max77819_pmic_i2c_id,
};

/*----------------------------------------------------------------------------*/
static struct i2c_client *max77819_pmic_i2c_client;
static struct platform_driver max77819_pmic_backlight_driver;
static struct max77819_pmic_i2c_data *obj_i2c_data;

/*----------------------------------------------------------------------------*/
#define BLS_TAG                  "[MAX77819_PMIC] "
#define BLS_FUN(f)               printk(KERN_ERR BLS_TAG"%s\n", __func__)
#define BLS_ERR(fmt, args...)    printk(KERN_ERR BLS_TAG"%s %d : "fmt, __func__, __LINE__, ##args)
#define BLS_LOG(fmt, args...)    printk(KERN_ERR BLS_TAG fmt, ##args)

/*----------------------------------------------------------------------------*/

/*-------------------- power control function----------------------------------*/

/******************************************************************************
 * Function Configuration
******************************************************************************/
static int max77819_pmic_open(struct inode *inode, struct file *file)
{
	file->private_data = max77819_pmic_i2c_client;

	if (file->private_data == NULL) {
		BLS_ERR("null pointer!!\n");
		return -EINVAL;
	}
	return nonseekable_open(inode, file);
}
/*----------------------------------------------------------------------------*/
static int max77819_pmic_release(struct inode *inode, struct file *file)
{
	file->private_data = NULL;
	return 0;
}

/*----------------------------------------------------------------------------*/
static int max77819_pmic_suspend(struct i2c_client *client, pm_message_t msg)
{
	struct max77819_pmic_i2c_data *obj = i2c_get_clientdata(client);
	int err = 0;
	kal_uint8  dat = 0;

	BLS_FUN();

	return err;
}
/*----------------------------------------------------------------------------*/
static int max77819_pmic_resume(struct i2c_client *client)
{
	struct max77819_pmic_i2c_data *obj = i2c_get_clientdata(client);
	int err;

	BLS_FUN();

	if (obj == NULL) {
		BLS_ERR("null pointer!!\n");
		return -EINVAL;
	}

	return 0;
}

static DEFINE_MUTEX(max77819_pmic_i2c_access);
/**********************************************************
  *
  *   [I2C Function For Read/Write max77819]
  *
  *********************************************************/

int max77819_pmic_read_byte(kal_uint8 cmd, kal_uint8 *returnData)
{
    char     cmd_buf = 0;
    char     readData = 0;
    int      ret = 0;

    BLS_FUN();
    mutex_lock(&max77819_pmic_i2c_access);

    cmd_buf = cmd;

    max77819_pmic_i2c_client->addr = (max77819_pmic_i2c_client->addr & I2C_MASK_FLAG) | I2C_WR_FLAG | I2C_RS_FLAG;
    ret = i2c_master_send(max77819_pmic_i2c_client, &cmd_buf, (1<<8 | 1));
    if (ret < 0) {
	BLS_ERR("send command error!!\n");
	mutex_unlock(&max77819_pmic_i2c_access);
	return ret;
    }

    readData = cmd_buf;
    *returnData = readData;

	max77819_pmic_i2c_client->addr = max77819_pmic_i2c_client->addr & I2C_MASK_FLAG;

    mutex_unlock(&max77819_pmic_i2c_access);
    return 0;
}

int max77819_pmic_write_byte(kal_uint8 cmd, kal_uint8 writeData)
{
    char    write_data[2] = {0};
    int     ret = 0;

	BLS_FUN();
    mutex_lock(&max77819_pmic_i2c_access);

    write_data[0] = cmd;
    write_data[1] = writeData;
    ret = i2c_master_send(max77819_pmic_i2c_client, write_data, 2);
    if (ret < 0) {
	BLS_ERR("send command error!!\n");
	mutex_unlock(&max77819_pmic_i2c_access);
	return ret;
    }
    mutex_unlock(&max77819_pmic_i2c_access);
    return 0;
}

u32 max77819_pmic_read_interface(kal_uint8 RegNum, kal_uint8 *val, kal_uint8 MASK, kal_uint8 SHIFT)
{
    kal_uint8 max77819_reg = 0;
    int ret = 0;

    ret = max77819_pmic_read_byte(RegNum, &max77819_reg);

        #ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
	BLS_LOG("[max77819_pmic_read_interface] Reg[%x]=0x%x\n", RegNum, max77819_reg);
	#endif

    max77819_reg &= (MASK << SHIFT);
    *val = (max77819_reg >> SHIFT);

        #ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
	BLS_LOG("[max77819_pmic_read_interface] val=0x%x\n", *val);
	#endif

    return ret;
}

u32 max77819_pmic_config_interface(kal_uint8 RegNum, kal_uint8 val, kal_uint8 MASK, kal_uint8 SHIFT)
{
    kal_uint8 max77819_pmic_reg = 0;
    int ret = 0;

    ret = max77819_pmic_read_byte(RegNum, &max77819_pmic_reg);
    #ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
    BLS_LOG("[max77819_pmic_config_interface] Reg[%x]=0x%x\n", RegNum, max77819_pmic_reg);
    #endif

    max77819_pmic_reg &= ~(MASK << SHIFT);
    max77819_pmic_reg |= (val << SHIFT);

    ret = max77819_pmic_write_byte(RegNum, max77819_pmic_reg);
    #ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
    BLS_LOG("[max77819_pmic_config_interface] write Reg[%x]=0x%x\n", RegNum, max77819_pmic_reg);
    #endif

    return ret;
}

static int max77819_pmic_read_byte_sr(struct i2c_client *client, kal_uint8 addr, kal_uint8 *data)
{
   kal_uint8 buf;
	int ret = 0;

	client->addr = (client->addr & I2C_MASK_FLAG) | I2C_WR_FLAG | I2C_RS_FLAG;
	buf = addr;
	ret = i2c_master_send(client, (const char *)&buf, 1<<8 | 1);
	if (ret < 0) {
		BLS_ERR("send command error!!\n");
		return ret;
	}

	*data = buf;
	client->addr = client->addr & I2C_MASK_FLAG;
	return 0;
}


int max77819_pmic_read_block(struct i2c_client *client, kal_uint8 addr, kal_uint8 *data, kal_uint8 len)
{
    if (len == 1) {
	return max77819_pmic_read_byte_sr(client, addr, data);
    } else {
	kal_uint8 beg = addr;
	struct i2c_msg msgs[2] = {
	{
		.addr = client->addr,    .flags = 0,
		.len = 1,                .buf = &beg
	    },
	{
		.addr = client->addr,    .flags = I2C_M_RD,
		.len = len,             .buf = data,
	}
	};
	int err;

	if (!client)
	return -EINVAL;
	else if (len > C_I2C_FIFO_SIZE) {
	    BLS_ERR(" length %d exceeds %d\n", len, C_I2C_FIFO_SIZE);
	return -EINVAL;
	}

	err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
	if (err != 2) {
	    BLS_ERR("i2c_transfer error: (%d %p %d) %d\n", addr, data, len, err);
	    err = -EIO;
	} else {
	    err = 0;    /*no error*/
	}
	return err;
    }

}
/*----------------------------------------------------------------------------*/
EXPORT_SYMBOL_GPL(max77819_pmic_read_block);


/*----------------------------------------------------------------------------*/
int max77819_pmic_write_block(struct i2c_client *client, kal_uint8 addr, kal_uint8 *data, kal_uint8 len)
{   /*because address also occupies one byte, the maximum length for write is 7 bytes*/
    int err, idx, num;
    char buf[C_I2C_FIFO_SIZE];

    if (!client)
	return -EINVAL;
    else if (len >= C_I2C_FIFO_SIZE) {
	BLS_ERR(" length %d exceeds %d\n", len, C_I2C_FIFO_SIZE);
	return -EINVAL;
    }

    num = 0;
    buf[num++] = addr;
    for (idx = 0; idx < len; idx++)
	buf[num++] = data[idx];

    err = i2c_master_send(client, buf, num);
    if (err < 0) {
	BLS_ERR("send command error!!\n");
	return -EFAULT;
    } else {
	err = 0;    /*no error*/
    }
    return err;
}
/*----------------------------------------------------------------------------*/
EXPORT_SYMBOL_GPL(max77819_pmic_write_block);

int max77819_pmic_reg_write(kal_uint8 reg, kal_uint8 val)
{
    kal_uint8 max77819_reg = 0;
    int ret = 0;

    max77819_pmic_read_byte(reg, &max77819_reg);

    #ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
    BLS_LOG("[max77819_pmic_reg_write] Before Reg[%x]=0x%x\n", reg, max77819_reg);
    #endif

    if (val == max77819_reg) {
	#ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
	BLS_LOG("[max77819_pmic_reg_write] Set Reg[%x] same value 0x%x\n", reg, max77819_reg);
	#endif
	return 0;
    }

	ret = max77819_pmic_write_byte(reg, val);
    #ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
    BLS_LOG("[max77819_pmic_reg_write] write Reg[%x]=0x%x\n", reg, val);
    #endif

    max77819_pmic_read_byte(reg, &max77819_reg);
    #ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
    BLS_LOG("[max77819_pmic_reg_write] After Reg[%x]=0x%x\n", reg, max77819_reg);
    #endif

    if (max77819_reg != val) {
	return -1;
    }
    return ret;
}
/*----------------------------------------------------------------------------*/
EXPORT_SYMBOL_GPL(max77819_pmic_reg_write);


int max77819_pmic_reg_read(kal_uint8 reg, kal_uint8 *val)
{
    int ret = 0;

    ret = max77819_pmic_read_byte(reg, val);
    #ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
    BLS_LOG("[max77819_pmic_reg_read] Reg[%x]=0x%x\n", reg, *val);
    #endif

    return ret;

}
EXPORT_SYMBOL_GPL(max77819_pmic_reg_read);


int max77819_pmic_reg_update_bits(kal_uint8 reg, kal_uint8 mask, kal_uint8 val)
{
    kal_uint8 max77819_reg = 0;
    kal_uint8 old_reg = 0;
    kal_uint8 new_reg = 0;
    int ret = 0;

    max77819_pmic_read_byte(reg, &max77819_reg);
    #ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
    BLS_LOG("[max77819_pmic_reg_update_bits] Before Reg[%x]=0x%x\n", reg, max77819_reg);
    #endif

    old_reg = max77819_reg;
    max77819_reg &= ~(mask);
    max77819_reg |= val;
    if (old_reg == max77819_reg) {
	#ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
	BLS_LOG("[max77819_pmic_reg_update_bits] Set Reg[%x] same value 0x%x\n", reg, max77819_reg);
	#endif
	return 0;
    }

    new_reg = max77819_reg;

    ret = max77819_pmic_write_byte(reg, max77819_reg);
    #ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
    BLS_LOG("[max77819_pmic_reg_update_bits] write Reg[%x]=0x%x\n", reg, max77819_reg);
    #endif

	max77819_pmic_read_byte(reg, &max77819_reg);
    #ifndef CONFIG_MEIZU_CLOSE_MTK_LOG
    BLS_LOG("[max77819_pmic_reg_update_bits] After Reg[%x]=0x%x\n", reg, max77819_reg);
    #endif

    if (max77819_reg != new_reg) {
	return -1;
    }
    return ret;

}
EXPORT_SYMBOL_GPL(max77819_pmic_reg_update_bits);


/*----------------------------------------------------------------------------*/
static int max77819_pmic_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	struct i2c_client *new_client;
	struct max77819_pmic_i2c_data *obj;

	int err = 0, i = 0;

	BLS_FUN();

	if (!(obj = kzalloc(sizeof(*obj), GFP_KERNEL))) {
		err = -1;
		goto exit;
	}

	memset(obj, 0, sizeof(struct max77819_pmic_i2c_data));

	obj_i2c_data = obj;
	obj->client = client;
	new_client = obj->client;
	i2c_set_clientdata(new_client, obj);

	atomic_set(&obj->trace, 0);
	atomic_set(&obj->suspend, 0);

	max77819_pmic_i2c_client = new_client;
	chargin_hw_init_done = KAL_TRUE;



kal_uint8 reg_val;
		for (i = 0x30 ; i <= 0x4b; i++)
			 max77819_pmic_reg_read(i, &reg_val);

exit:

	return err;
}

/*----------------------------------------------------------------------------*/
static int max77819_pmic_i2c_remove(struct i2c_client *client)
{
	int err = 0;

	max77819_pmic_i2c_client = NULL;
	i2c_unregister_device(client);
	kfree(i2c_get_clientdata(client));
	return 0;
}


/*----------------------------------------------------------------------------*/
static int __init max77819_pmic_init(void)
{
	BLS_FUN();
	i2c_register_board_info(/*2*/MAX77819_PMIC_I2C_NUM, &i2c_max77819_pmic, 1);
	if (i2c_add_driver(&max77819_pmic_i2c_driver)) {
		BLS_ERR("add driver error\n");
		return -1;
	}

	return 0;
}
/*----------------------------------------------------------------------------*/
static void __exit max77819_pmic_exit(void)
{
	BLS_FUN();
	i2c_del_driver(&max77819_pmic_i2c_driver);
}
/*----------------------------------------------------------------------------*/
module_init(max77819_pmic_init);
module_exit(max77819_pmic_exit);
/*----------------------------------------------------------------------------*/
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("MAX77819_PMIC I2C driver");
MODULE_AUTHOR("Bing.Song@mediatek.com");