ASoC: Intel: sst: Remove unused function sst_restore_shim64()
[sfrench/cifs-2.6.git] / sound / soc / intel / atom / sst / sst.c
1 /*
2  *  sst.c - Intel SST Driver for audio engine
3  *
4  *  Copyright (C) 2008-14       Intel Corp
5  *  Authors:    Vinod Koul <vinod.koul@intel.com>
6  *              Harsha Priya <priya.harsha@intel.com>
7  *              Dharageswari R <dharageswari.r@intel.com>
8  *              KP Jeeja <jeeja.kp@intel.com>
9  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; version 2 of the License.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21  */
22 #include <linux/module.h>
23 #include <linux/fs.h>
24 #include <linux/interrupt.h>
25 #include <linux/firmware.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/pm_qos.h>
28 #include <linux/async.h>
29 #include <linux/acpi.h>
30 #include <linux/sysfs.h>
31 #include <sound/core.h>
32 #include <sound/soc.h>
33 #include <asm/platform_sst_audio.h>
34 #include "../sst-mfld-platform.h"
35 #include "sst.h"
36 #include "../../common/sst-dsp.h"
37
38 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
39 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
40 MODULE_DESCRIPTION("Intel (R) SST(R) Audio Engine Driver");
41 MODULE_LICENSE("GPL v2");
42
43 static inline bool sst_is_process_reply(u32 msg_id)
44 {
45         return ((msg_id & PROCESS_MSG) ? true : false);
46 }
47
48 static inline bool sst_validate_mailbox_size(unsigned int size)
49 {
50         return ((size <= SST_MAILBOX_SIZE) ? true : false);
51 }
52
53 static irqreturn_t intel_sst_interrupt_mrfld(int irq, void *context)
54 {
55         union interrupt_reg_mrfld isr;
56         union ipc_header_mrfld header;
57         union sst_imr_reg_mrfld imr;
58         struct ipc_post *msg = NULL;
59         unsigned int size = 0;
60         struct intel_sst_drv *drv = (struct intel_sst_drv *) context;
61         irqreturn_t retval = IRQ_HANDLED;
62
63         /* Interrupt arrived, check src */
64         isr.full = sst_shim_read64(drv->shim, SST_ISRX);
65
66         if (isr.part.done_interrupt) {
67                 /* Clear done bit */
68                 spin_lock(&drv->ipc_spin_lock);
69                 header.full = sst_shim_read64(drv->shim,
70                                         drv->ipc_reg.ipcx);
71                 header.p.header_high.part.done = 0;
72                 sst_shim_write64(drv->shim, drv->ipc_reg.ipcx, header.full);
73
74                 /* write 1 to clear status register */;
75                 isr.part.done_interrupt = 1;
76                 sst_shim_write64(drv->shim, SST_ISRX, isr.full);
77                 spin_unlock(&drv->ipc_spin_lock);
78
79                 /* we can send more messages to DSP so trigger work */
80                 queue_work(drv->post_msg_wq, &drv->ipc_post_msg_wq);
81                 retval = IRQ_HANDLED;
82         }
83
84         if (isr.part.busy_interrupt) {
85                 /* message from dsp so copy that */
86                 spin_lock(&drv->ipc_spin_lock);
87                 imr.full = sst_shim_read64(drv->shim, SST_IMRX);
88                 imr.part.busy_interrupt = 1;
89                 sst_shim_write64(drv->shim, SST_IMRX, imr.full);
90                 spin_unlock(&drv->ipc_spin_lock);
91                 header.full =  sst_shim_read64(drv->shim, drv->ipc_reg.ipcd);
92
93                 if (sst_create_ipc_msg(&msg, header.p.header_high.part.large)) {
94                         drv->ops->clear_interrupt(drv);
95                         return IRQ_HANDLED;
96                 }
97
98                 if (header.p.header_high.part.large) {
99                         size = header.p.header_low_payload;
100                         if (sst_validate_mailbox_size(size)) {
101                                 memcpy_fromio(msg->mailbox_data,
102                                         drv->mailbox + drv->mailbox_recv_offset, size);
103                         } else {
104                                 dev_err(drv->dev,
105                                         "Mailbox not copied, payload size is: %u\n", size);
106                                 header.p.header_low_payload = 0;
107                         }
108                 }
109
110                 msg->mrfld_header = header;
111                 msg->is_process_reply =
112                         sst_is_process_reply(header.p.header_high.part.msg_id);
113                 spin_lock(&drv->rx_msg_lock);
114                 list_add_tail(&msg->node, &drv->rx_list);
115                 spin_unlock(&drv->rx_msg_lock);
116                 drv->ops->clear_interrupt(drv);
117                 retval = IRQ_WAKE_THREAD;
118         }
119         return retval;
120 }
121
122 static irqreturn_t intel_sst_irq_thread_mrfld(int irq, void *context)
123 {
124         struct intel_sst_drv *drv = (struct intel_sst_drv *) context;
125         struct ipc_post *__msg, *msg = NULL;
126         unsigned long irq_flags;
127
128         spin_lock_irqsave(&drv->rx_msg_lock, irq_flags);
129         if (list_empty(&drv->rx_list)) {
130                 spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
131                 return IRQ_HANDLED;
132         }
133
134         list_for_each_entry_safe(msg, __msg, &drv->rx_list, node) {
135                 list_del(&msg->node);
136                 spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
137                 if (msg->is_process_reply)
138                         drv->ops->process_message(msg);
139                 else
140                         drv->ops->process_reply(drv, msg);
141
142                 if (msg->is_large)
143                         kfree(msg->mailbox_data);
144                 kfree(msg);
145                 spin_lock_irqsave(&drv->rx_msg_lock, irq_flags);
146         }
147         spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
148         return IRQ_HANDLED;
149 }
150
151 static int sst_save_dsp_context_v2(struct intel_sst_drv *sst)
152 {
153         int ret = 0;
154
155         ret = sst_prepare_and_post_msg(sst, SST_TASK_ID_MEDIA, IPC_CMD,
156                         IPC_PREP_D3, PIPE_RSVD, 0, NULL, NULL,
157                         true, true, false, true);
158
159         if (ret < 0) {
160                 dev_err(sst->dev, "not suspending FW!!, Err: %d\n", ret);
161                 return -EIO;
162         }
163
164         return 0;
165 }
166
167
168 static struct intel_sst_ops mrfld_ops = {
169         .interrupt = intel_sst_interrupt_mrfld,
170         .irq_thread = intel_sst_irq_thread_mrfld,
171         .clear_interrupt = intel_sst_clear_intr_mrfld,
172         .start = sst_start_mrfld,
173         .reset = intel_sst_reset_dsp_mrfld,
174         .post_message = sst_post_message_mrfld,
175         .process_reply = sst_process_reply_mrfld,
176         .save_dsp_context =  sst_save_dsp_context_v2,
177         .alloc_stream = sst_alloc_stream_mrfld,
178         .post_download = sst_post_download_mrfld,
179 };
180
181 int sst_driver_ops(struct intel_sst_drv *sst)
182 {
183
184         switch (sst->dev_id) {
185         case SST_MRFLD_PCI_ID:
186         case SST_BYT_ACPI_ID:
187         case SST_CHV_ACPI_ID:
188                 sst->tstamp = SST_TIME_STAMP_MRFLD;
189                 sst->ops = &mrfld_ops;
190                 return 0;
191
192         default:
193                 dev_err(sst->dev,
194                         "SST Driver capabilities missing for dev_id: %x",
195                         sst->dev_id);
196                 return -EINVAL;
197         };
198 }
199
200 void sst_process_pending_msg(struct work_struct *work)
201 {
202         struct intel_sst_drv *ctx = container_of(work,
203                         struct intel_sst_drv, ipc_post_msg_wq);
204
205         ctx->ops->post_message(ctx, NULL, false);
206 }
207
208 static int sst_workqueue_init(struct intel_sst_drv *ctx)
209 {
210         INIT_LIST_HEAD(&ctx->memcpy_list);
211         INIT_LIST_HEAD(&ctx->rx_list);
212         INIT_LIST_HEAD(&ctx->ipc_dispatch_list);
213         INIT_LIST_HEAD(&ctx->block_list);
214         INIT_WORK(&ctx->ipc_post_msg_wq, sst_process_pending_msg);
215         init_waitqueue_head(&ctx->wait_queue);
216
217         ctx->post_msg_wq =
218                 create_singlethread_workqueue("sst_post_msg_wq");
219         if (!ctx->post_msg_wq)
220                 return -EBUSY;
221         return 0;
222 }
223
224 static void sst_init_locks(struct intel_sst_drv *ctx)
225 {
226         mutex_init(&ctx->sst_lock);
227         spin_lock_init(&ctx->rx_msg_lock);
228         spin_lock_init(&ctx->ipc_spin_lock);
229         spin_lock_init(&ctx->block_lock);
230 }
231
232 int sst_alloc_drv_context(struct intel_sst_drv **ctx,
233                 struct device *dev, unsigned int dev_id)
234 {
235         *ctx = devm_kzalloc(dev, sizeof(struct intel_sst_drv), GFP_KERNEL);
236         if (!(*ctx))
237                 return -ENOMEM;
238
239         (*ctx)->dev = dev;
240         (*ctx)->dev_id = dev_id;
241
242         return 0;
243 }
244 EXPORT_SYMBOL_GPL(sst_alloc_drv_context);
245
246 static ssize_t firmware_version_show(struct device *dev,
247                             struct device_attribute *attr, char *buf)
248 {
249         struct intel_sst_drv *ctx = dev_get_drvdata(dev);
250
251         if (ctx->fw_version.type == 0 && ctx->fw_version.major == 0 &&
252             ctx->fw_version.minor == 0 && ctx->fw_version.build == 0)
253                 return sprintf(buf, "FW not yet loaded\n");
254         else
255                 return sprintf(buf, "v%02x.%02x.%02x.%02x\n",
256                                ctx->fw_version.type, ctx->fw_version.major,
257                                ctx->fw_version.minor, ctx->fw_version.build);
258
259 }
260
261 static DEVICE_ATTR_RO(firmware_version);
262
263 static const struct attribute *sst_fw_version_attrs[] = {
264         &dev_attr_firmware_version.attr,
265         NULL,
266 };
267
268 static const struct attribute_group sst_fw_version_attr_group = {
269         .attrs = (struct attribute **)sst_fw_version_attrs,
270 };
271
272 int sst_context_init(struct intel_sst_drv *ctx)
273 {
274         int ret = 0, i;
275
276         if (!ctx->pdata)
277                 return -EINVAL;
278
279         if (!ctx->pdata->probe_data)
280                 return -EINVAL;
281
282         memcpy(&ctx->info, ctx->pdata->probe_data, sizeof(ctx->info));
283
284         ret = sst_driver_ops(ctx);
285         if (ret != 0)
286                 return -EINVAL;
287
288         sst_init_locks(ctx);
289         sst_set_fw_state_locked(ctx, SST_RESET);
290
291         /* pvt_id 0 reserved for async messages */
292         ctx->pvt_id = 1;
293         ctx->stream_cnt = 0;
294         ctx->fw_in_mem = NULL;
295         /* we use memcpy, so set to 0 */
296         ctx->use_dma = 0;
297         ctx->use_lli = 0;
298
299         if (sst_workqueue_init(ctx))
300                 return -EINVAL;
301
302         ctx->mailbox_recv_offset = ctx->pdata->ipc_info->mbox_recv_off;
303         ctx->ipc_reg.ipcx = SST_IPCX + ctx->pdata->ipc_info->ipc_offset;
304         ctx->ipc_reg.ipcd = SST_IPCD + ctx->pdata->ipc_info->ipc_offset;
305
306         dev_info(ctx->dev, "Got drv data max stream %d\n",
307                                 ctx->info.max_streams);
308
309         for (i = 1; i <= ctx->info.max_streams; i++) {
310                 struct stream_info *stream = &ctx->streams[i];
311
312                 memset(stream, 0, sizeof(*stream));
313                 stream->pipe_id = PIPE_RSVD;
314                 mutex_init(&stream->lock);
315         }
316
317         /* Register the ISR */
318         ret = devm_request_threaded_irq(ctx->dev, ctx->irq_num, ctx->ops->interrupt,
319                                         ctx->ops->irq_thread, 0, SST_DRV_NAME,
320                                         ctx);
321         if (ret)
322                 goto do_free_mem;
323
324         dev_dbg(ctx->dev, "Registered IRQ %#x\n", ctx->irq_num);
325
326         /* default intr are unmasked so set this as masked */
327         sst_shim_write64(ctx->shim, SST_IMRX, 0xFFFF0038);
328
329         ctx->qos = devm_kzalloc(ctx->dev,
330                 sizeof(struct pm_qos_request), GFP_KERNEL);
331         if (!ctx->qos) {
332                 ret = -ENOMEM;
333                 goto do_free_mem;
334         }
335         pm_qos_add_request(ctx->qos, PM_QOS_CPU_DMA_LATENCY,
336                                 PM_QOS_DEFAULT_VALUE);
337
338         dev_dbg(ctx->dev, "Requesting FW %s now...\n", ctx->firmware_name);
339         ret = request_firmware_nowait(THIS_MODULE, true, ctx->firmware_name,
340                                       ctx->dev, GFP_KERNEL, ctx, sst_firmware_load_cb);
341         if (ret) {
342                 dev_err(ctx->dev, "Firmware download failed:%d\n", ret);
343                 goto do_free_mem;
344         }
345
346         ret = sysfs_create_group(&ctx->dev->kobj,
347                                  &sst_fw_version_attr_group);
348         if (ret) {
349                 dev_err(ctx->dev,
350                         "Unable to create sysfs\n");
351                 goto err_sysfs;
352         }
353
354         sst_register(ctx->dev);
355         return 0;
356 err_sysfs:
357         sysfs_remove_group(&ctx->dev->kobj, &sst_fw_version_attr_group);
358
359 do_free_mem:
360         destroy_workqueue(ctx->post_msg_wq);
361         return ret;
362 }
363 EXPORT_SYMBOL_GPL(sst_context_init);
364
365 void sst_context_cleanup(struct intel_sst_drv *ctx)
366 {
367         pm_runtime_get_noresume(ctx->dev);
368         pm_runtime_disable(ctx->dev);
369         sst_unregister(ctx->dev);
370         sst_set_fw_state_locked(ctx, SST_SHUTDOWN);
371         sysfs_remove_group(&ctx->dev->kobj, &sst_fw_version_attr_group);
372         flush_scheduled_work();
373         destroy_workqueue(ctx->post_msg_wq);
374         pm_qos_remove_request(ctx->qos);
375         kfree(ctx->fw_sg_list.src);
376         kfree(ctx->fw_sg_list.dst);
377         ctx->fw_sg_list.list_len = 0;
378         kfree(ctx->fw_in_mem);
379         ctx->fw_in_mem = NULL;
380         sst_memcpy_free_resources(ctx);
381         ctx = NULL;
382 }
383 EXPORT_SYMBOL_GPL(sst_context_cleanup);
384
385 static inline void sst_save_shim64(struct intel_sst_drv *ctx,
386                             void __iomem *shim,
387                             struct sst_shim_regs64 *shim_regs)
388 {
389         unsigned long irq_flags;
390
391         spin_lock_irqsave(&ctx->ipc_spin_lock, irq_flags);
392
393         shim_regs->imrx = sst_shim_read64(shim, SST_IMRX);
394         shim_regs->csr = sst_shim_read64(shim, SST_CSR);
395
396
397         spin_unlock_irqrestore(&ctx->ipc_spin_lock, irq_flags);
398 }
399
400 void sst_configure_runtime_pm(struct intel_sst_drv *ctx)
401 {
402         pm_runtime_set_autosuspend_delay(ctx->dev, SST_SUSPEND_DELAY);
403         pm_runtime_use_autosuspend(ctx->dev);
404         /*
405          * For acpi devices, the actual physical device state is
406          * initially active. So change the state to active before
407          * enabling the pm
408          */
409
410         if (!acpi_disabled)
411                 pm_runtime_set_active(ctx->dev);
412
413         pm_runtime_enable(ctx->dev);
414
415         if (acpi_disabled)
416                 pm_runtime_set_active(ctx->dev);
417         else
418                 pm_runtime_put_noidle(ctx->dev);
419
420         sst_save_shim64(ctx, ctx->shim, ctx->shim_regs64);
421 }
422 EXPORT_SYMBOL_GPL(sst_configure_runtime_pm);
423
424 static int intel_sst_runtime_suspend(struct device *dev)
425 {
426         int ret = 0;
427         struct intel_sst_drv *ctx = dev_get_drvdata(dev);
428
429         if (ctx->sst_state == SST_RESET) {
430                 dev_dbg(dev, "LPE is already in RESET state, No action\n");
431                 return 0;
432         }
433         /* save fw context */
434         if (ctx->ops->save_dsp_context(ctx))
435                 return -EBUSY;
436
437         /* Move the SST state to Reset */
438         sst_set_fw_state_locked(ctx, SST_RESET);
439
440         synchronize_irq(ctx->irq_num);
441         flush_workqueue(ctx->post_msg_wq);
442
443         ctx->ops->reset(ctx);
444         /* save the shim registers because PMC doesn't save state */
445         sst_save_shim64(ctx, ctx->shim, ctx->shim_regs64);
446
447         return ret;
448 }
449
450 static int intel_sst_suspend(struct device *dev)
451 {
452         struct intel_sst_drv *ctx = dev_get_drvdata(dev);
453         struct sst_fw_save *fw_save;
454         int i, ret = 0;
455
456         /* check first if we are already in SW reset */
457         if (ctx->sst_state == SST_RESET)
458                 return 0;
459
460         /*
461          * check if any stream is active and running
462          * they should already by suspend by soc_suspend
463          */
464         for (i = 1; i <= ctx->info.max_streams; i++) {
465                 struct stream_info *stream = &ctx->streams[i];
466
467                 if (stream->status == STREAM_RUNNING) {
468                         dev_err(dev, "stream %d is running, can't suspend, abort\n", i);
469                         return -EBUSY;
470                 }
471         }
472         synchronize_irq(ctx->irq_num);
473         flush_workqueue(ctx->post_msg_wq);
474
475         /* Move the SST state to Reset */
476         sst_set_fw_state_locked(ctx, SST_RESET);
477
478         /* tell DSP we are suspending */
479         if (ctx->ops->save_dsp_context(ctx))
480                 return -EBUSY;
481
482         /* save the memories */
483         fw_save = kzalloc(sizeof(*fw_save), GFP_KERNEL);
484         if (!fw_save)
485                 return -ENOMEM;
486         fw_save->iram = kzalloc(ctx->iram_end - ctx->iram_base, GFP_KERNEL);
487         if (!fw_save->iram) {
488                 ret = -ENOMEM;
489                 goto iram;
490         }
491         fw_save->dram = kzalloc(ctx->dram_end - ctx->dram_base, GFP_KERNEL);
492         if (!fw_save->dram) {
493                 ret = -ENOMEM;
494                 goto dram;
495         }
496         fw_save->sram = kzalloc(SST_MAILBOX_SIZE, GFP_KERNEL);
497         if (!fw_save->sram) {
498                 ret = -ENOMEM;
499                 goto sram;
500         }
501
502         fw_save->ddr = kzalloc(ctx->ddr_end - ctx->ddr_base, GFP_KERNEL);
503         if (!fw_save->ddr) {
504                 ret = -ENOMEM;
505                 goto ddr;
506         }
507
508         memcpy32_fromio(fw_save->iram, ctx->iram, ctx->iram_end - ctx->iram_base);
509         memcpy32_fromio(fw_save->dram, ctx->dram, ctx->dram_end - ctx->dram_base);
510         memcpy32_fromio(fw_save->sram, ctx->mailbox, SST_MAILBOX_SIZE);
511         memcpy32_fromio(fw_save->ddr, ctx->ddr, ctx->ddr_end - ctx->ddr_base);
512
513         ctx->fw_save = fw_save;
514         ctx->ops->reset(ctx);
515         return 0;
516 ddr:
517         kfree(fw_save->sram);
518 sram:
519         kfree(fw_save->dram);
520 dram:
521         kfree(fw_save->iram);
522 iram:
523         kfree(fw_save);
524         return ret;
525 }
526
527 static int intel_sst_resume(struct device *dev)
528 {
529         struct intel_sst_drv *ctx = dev_get_drvdata(dev);
530         struct sst_fw_save *fw_save = ctx->fw_save;
531         int ret = 0;
532         struct sst_block *block;
533
534         if (!fw_save)
535                 return 0;
536
537         sst_set_fw_state_locked(ctx, SST_FW_LOADING);
538
539         /* we have to restore the memory saved */
540         ctx->ops->reset(ctx);
541
542         ctx->fw_save = NULL;
543
544         memcpy32_toio(ctx->iram, fw_save->iram, ctx->iram_end - ctx->iram_base);
545         memcpy32_toio(ctx->dram, fw_save->dram, ctx->dram_end - ctx->dram_base);
546         memcpy32_toio(ctx->mailbox, fw_save->sram, SST_MAILBOX_SIZE);
547         memcpy32_toio(ctx->ddr, fw_save->ddr, ctx->ddr_end - ctx->ddr_base);
548
549         kfree(fw_save->sram);
550         kfree(fw_save->dram);
551         kfree(fw_save->iram);
552         kfree(fw_save->ddr);
553         kfree(fw_save);
554
555         block = sst_create_block(ctx, 0, FW_DWNL_ID);
556         if (block == NULL)
557                 return -ENOMEM;
558
559
560         /* start and wait for ack */
561         ctx->ops->start(ctx);
562         ret = sst_wait_timeout(ctx, block);
563         if (ret) {
564                 dev_err(ctx->dev, "fw download failed %d\n", ret);
565                 /* FW download failed due to timeout */
566                 ret = -EBUSY;
567
568         } else {
569                 sst_set_fw_state_locked(ctx, SST_FW_RUNNING);
570         }
571
572         sst_free_block(ctx, block);
573         return ret;
574 }
575
576 const struct dev_pm_ops intel_sst_pm = {
577         .suspend = intel_sst_suspend,
578         .resume = intel_sst_resume,
579         .runtime_suspend = intel_sst_runtime_suspend,
580 };
581 EXPORT_SYMBOL_GPL(intel_sst_pm);