/arch/arm/mach-msm/board-mahimahi-tpa2018d1.c

https://github.com/AICP/kernel_google_msm · C · 368 lines · 297 code · 52 blank · 19 comment · 39 complexity · 8e0f472233dd5bbed2cf1736ddf27d63 MD5 · raw file

  1. /* drivers/i2c/chips/tpa2018d1.c
  2. *
  3. * TI TPA2018D1 Speaker Amplifier
  4. *
  5. * Copyright (C) 2009 HTC Corporation
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. /* TODO: content validation in TPA2018_SET_CONFIG */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/device.h>
  21. #include <linux/fs.h>
  22. #include <linux/i2c.h>
  23. #include <linux/miscdevice.h>
  24. #include <linux/gpio.h>
  25. #include <asm/uaccess.h>
  26. #include <linux/delay.h>
  27. #include <linux/mutex.h>
  28. #include <linux/slab.h>
  29. #include <linux/tpa2018d1.h>
  30. #include "board-mahimahi-tpa2018d1.h"
  31. static struct i2c_client *this_client;
  32. static struct tpa2018d1_platform_data *pdata;
  33. static int is_on;
  34. static char spk_amp_cfg[8];
  35. static const char spk_amp_on[8] = { /* same length as spk_amp_cfg */
  36. 0x01, 0xc3, 0x20, 0x01, 0x00, 0x08, 0x1a, 0x21
  37. };
  38. static const char spk_amp_off[] = {0x01, 0xa2};
  39. static DEFINE_MUTEX(spk_amp_lock);
  40. static int tpa2018d1_opened;
  41. static char *config_data;
  42. static int tpa2018d1_num_modes;
  43. #define DEBUG 0
  44. static int tpa2018_i2c_write(const char *txData, int length)
  45. {
  46. struct i2c_msg msg[] = {
  47. {
  48. .addr = this_client->addr,
  49. .flags = 0,
  50. .len = length,
  51. .buf = txData,
  52. },
  53. };
  54. if (i2c_transfer(this_client->adapter, msg, 1) < 0) {
  55. pr_err("%s: I2C transfer error\n", __func__);
  56. return -EIO;
  57. } else
  58. return 0;
  59. }
  60. static int tpa2018_i2c_read(char *rxData, int length)
  61. {
  62. struct i2c_msg msgs[] = {
  63. {
  64. .addr = this_client->addr,
  65. .flags = I2C_M_RD,
  66. .len = length,
  67. .buf = rxData,
  68. },
  69. };
  70. if (i2c_transfer(this_client->adapter, msgs, 1) < 0) {
  71. pr_err("%s: I2C transfer error\n", __func__);
  72. return -EIO;
  73. }
  74. #if DEBUG
  75. do {
  76. int i = 0;
  77. for (i = 0; i < length; i++)
  78. pr_info("%s: rx[%d] = %2x\n",
  79. __func__, i, rxData[i]);
  80. } while(0);
  81. #endif
  82. return 0;
  83. }
  84. static int tpa2018d1_open(struct inode *inode, struct file *file)
  85. {
  86. int rc = 0;
  87. mutex_lock(&spk_amp_lock);
  88. if (tpa2018d1_opened) {
  89. pr_err("%s: busy\n", __func__);
  90. rc = -EBUSY;
  91. goto done;
  92. }
  93. tpa2018d1_opened = 1;
  94. done:
  95. mutex_unlock(&spk_amp_lock);
  96. return rc;
  97. }
  98. static int tpa2018d1_release(struct inode *inode, struct file *file)
  99. {
  100. mutex_lock(&spk_amp_lock);
  101. tpa2018d1_opened = 0;
  102. mutex_unlock(&spk_amp_lock);
  103. return 0;
  104. }
  105. static int tpa2018d1_read_config(void __user *argp)
  106. {
  107. int rc = 0;
  108. unsigned char reg_idx = 0x01;
  109. unsigned char tmp[7];
  110. if (!is_on) {
  111. gpio_set_value(pdata->gpio_tpa2018_spk_en, 1);
  112. msleep(5); /* According to TPA2018D1 Spec */
  113. }
  114. rc = tpa2018_i2c_write(&reg_idx, sizeof(reg_idx));
  115. if (rc < 0)
  116. goto err;
  117. rc = tpa2018_i2c_read(tmp, sizeof(tmp));
  118. if (rc < 0)
  119. goto err;
  120. if (copy_to_user(argp, &tmp, sizeof(tmp)))
  121. rc = -EFAULT;
  122. err:
  123. if (!is_on)
  124. gpio_set_value(pdata->gpio_tpa2018_spk_en, 0);
  125. return rc;
  126. }
  127. static int tpa2018d1_ioctl(struct inode *inode, struct file *file,
  128. unsigned int cmd, unsigned long arg)
  129. {
  130. void __user *argp = (void __user *)arg;
  131. int rc = 0;
  132. int mode = -1;
  133. int offset = 0;
  134. struct tpa2018d1_config_data cfg;
  135. mutex_lock(&spk_amp_lock);
  136. switch (cmd) {
  137. case TPA2018_SET_CONFIG:
  138. if (copy_from_user(spk_amp_cfg, argp, sizeof(spk_amp_cfg)))
  139. rc = -EFAULT;
  140. break;
  141. case TPA2018_READ_CONFIG:
  142. rc = tpa2018d1_read_config(argp);
  143. break;
  144. case TPA2018_SET_MODE:
  145. if (copy_from_user(&mode, argp, sizeof(mode))) {
  146. rc = -EFAULT;
  147. break;
  148. }
  149. if (mode >= tpa2018d1_num_modes || mode < 0) {
  150. pr_err("%s: unsupported tpa2018d1 mode %d\n",
  151. __func__, mode);
  152. rc = -EINVAL;
  153. break;
  154. }
  155. if (!config_data) {
  156. pr_err("%s: no config data!\n", __func__);
  157. rc = -EIO;
  158. break;
  159. }
  160. memcpy(spk_amp_cfg, config_data + mode * TPA2018D1_CMD_LEN,
  161. TPA2018D1_CMD_LEN);
  162. break;
  163. case TPA2018_SET_PARAM:
  164. if (copy_from_user(&cfg, argp, sizeof(cfg))) {
  165. pr_err("%s: copy from user failed.\n", __func__);
  166. rc = -EFAULT;
  167. break;
  168. }
  169. tpa2018d1_num_modes = cfg.mode_num;
  170. if (tpa2018d1_num_modes > TPA2018_NUM_MODES) {
  171. pr_err("%s: invalid number of modes %d\n", __func__,
  172. tpa2018d1_num_modes);
  173. rc = -EINVAL;
  174. break;
  175. }
  176. if (cfg.data_len != tpa2018d1_num_modes*TPA2018D1_CMD_LEN) {
  177. pr_err("%s: invalid data length %d, expecting %d\n",
  178. __func__, cfg.data_len,
  179. tpa2018d1_num_modes * TPA2018D1_CMD_LEN);
  180. rc = -EINVAL;
  181. break;
  182. }
  183. /* Free the old data */
  184. if (config_data)
  185. kfree(config_data);
  186. config_data = kmalloc(cfg.data_len, GFP_KERNEL);
  187. if (!config_data) {
  188. pr_err("%s: out of memory\n", __func__);
  189. rc = -ENOMEM;
  190. break;
  191. }
  192. if (copy_from_user(config_data, cfg.cmd_data, cfg.data_len)) {
  193. pr_err("%s: copy data from user failed.\n", __func__);
  194. kfree(config_data);
  195. config_data = NULL;
  196. rc = -EFAULT;
  197. break;
  198. }
  199. /* replace default setting with playback setting */
  200. if (tpa2018d1_num_modes >= TPA2018_MODE_PLAYBACK) {
  201. offset = TPA2018_MODE_PLAYBACK * TPA2018D1_CMD_LEN;
  202. memcpy(spk_amp_cfg, config_data + offset,
  203. TPA2018D1_CMD_LEN);
  204. }
  205. break;
  206. default:
  207. pr_err("%s: invalid command %d\n", __func__, _IOC_NR(cmd));
  208. rc = -EINVAL;
  209. break;
  210. }
  211. mutex_unlock(&spk_amp_lock);
  212. return rc;
  213. }
  214. static struct file_operations tpa2018d1_fops = {
  215. .owner = THIS_MODULE,
  216. .open = tpa2018d1_open,
  217. .release = tpa2018d1_release,
  218. .ioctl = tpa2018d1_ioctl,
  219. };
  220. static struct miscdevice tpa2018d1_device = {
  221. .minor = MISC_DYNAMIC_MINOR,
  222. .name = "tpa2018d1",
  223. .fops = &tpa2018d1_fops,
  224. };
  225. void tpa2018d1_set_speaker_amp(int on)
  226. {
  227. if (!pdata) {
  228. pr_err("%s: no platform data!\n", __func__);
  229. return;
  230. }
  231. mutex_lock(&spk_amp_lock);
  232. if (on && !is_on) {
  233. gpio_set_value(pdata->gpio_tpa2018_spk_en, 1);
  234. msleep(5); /* According to TPA2018D1 Spec */
  235. if (tpa2018_i2c_write(spk_amp_cfg, sizeof(spk_amp_cfg)) == 0) {
  236. is_on = 1;
  237. pr_info("%s: ON\n", __func__);
  238. }
  239. } else if (!on && is_on) {
  240. if (tpa2018_i2c_write(spk_amp_off, sizeof(spk_amp_off)) == 0) {
  241. is_on = 0;
  242. msleep(2);
  243. gpio_set_value(pdata->gpio_tpa2018_spk_en, 0);
  244. pr_info("%s: OFF\n", __func__);
  245. }
  246. }
  247. mutex_unlock(&spk_amp_lock);
  248. }
  249. static int tpa2018d1_probe(struct i2c_client *client, const struct i2c_device_id *id)
  250. {
  251. int ret = 0;
  252. pdata = client->dev.platform_data;
  253. if (!pdata) {
  254. ret = -EINVAL;
  255. pr_err("%s: platform data is NULL\n", __func__);
  256. goto err_no_pdata;
  257. }
  258. this_client = client;
  259. ret = gpio_request(pdata->gpio_tpa2018_spk_en, "tpa2018");
  260. if (ret < 0) {
  261. pr_err("%s: gpio request aud_spk_en pin failed\n", __func__);
  262. goto err_free_gpio;
  263. }
  264. ret = gpio_direction_output(pdata->gpio_tpa2018_spk_en, 1);
  265. if (ret < 0) {
  266. pr_err("%s: request aud_spk_en gpio direction failed\n",
  267. __func__);
  268. goto err_free_gpio;
  269. }
  270. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  271. pr_err("%s: i2c check functionality error\n", __func__);
  272. ret = -ENODEV;
  273. goto err_free_gpio;
  274. }
  275. gpio_set_value(pdata->gpio_tpa2018_spk_en, 0); /* Default Low */
  276. ret = misc_register(&tpa2018d1_device);
  277. if (ret) {
  278. pr_err("%s: tpa2018d1_device register failed\n", __func__);
  279. goto err_free_gpio;
  280. }
  281. memcpy(spk_amp_cfg, spk_amp_on, sizeof(spk_amp_on));
  282. return 0;
  283. err_free_gpio:
  284. gpio_free(pdata->gpio_tpa2018_spk_en);
  285. err_no_pdata:
  286. return ret;
  287. }
  288. static int tpa2018d1_suspend(struct i2c_client *client, pm_message_t mesg)
  289. {
  290. return 0;
  291. }
  292. static int tpa2018d1_resume(struct i2c_client *client)
  293. {
  294. return 0;
  295. }
  296. static const struct i2c_device_id tpa2018d1_id[] = {
  297. { TPA2018D1_I2C_NAME, 0 },
  298. { }
  299. };
  300. static struct i2c_driver tpa2018d1_driver = {
  301. .probe = tpa2018d1_probe,
  302. .suspend = tpa2018d1_suspend,
  303. .resume = tpa2018d1_resume,
  304. .id_table = tpa2018d1_id,
  305. .driver = {
  306. .name = TPA2018D1_I2C_NAME,
  307. },
  308. };
  309. static int __init tpa2018d1_init(void)
  310. {
  311. pr_info("%s\n", __func__);
  312. return i2c_add_driver(&tpa2018d1_driver);
  313. }
  314. module_init(tpa2018d1_init);
  315. MODULE_DESCRIPTION("tpa2018d1 speaker amp driver");
  316. MODULE_LICENSE("GPL");